Resultのインポート方法

@praha/byethrow は、効率的な開発と学習コストのバランスを取るために、2つの異なるインポート方法を提供しています。 どちらのアプローチもTree-shakingを完全にサポートしており、未使用の機能は最終バンドルから自動的に除外されます。

2つのインポート方法

明示的な名前空間アプローチ(Result

明確さを重視するコードには、Result を使用します。

import { 
import Result
Result
} from '@praha/byethrow';
const
const validateUser: (id: string) => Result.Failure<Error> | Result.Success<string>
validateUser
= (
id: string
id
: string) => {
if (!
id: string
id
.
String.startsWith(searchString: string, position?: number): boolean

Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.

startsWith
('u')) {
return
import Result
Result
.
const fail: <Error>(error: Error) => Result.Result<never, Error> (+1 overload)
fail
(new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('Invalid ID format'));
} return
import Result
Result
.
const succeed: <string>(value: string) => Result.Result<string, never> (+1 overload)
succeed
(
id: string
id
);
}; const
const result: Result.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>
result
=
import Result
Result
.
const pipe: <Result.Result<"u123", never>, Result.Result<string, Error>, Result.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>>(a: Result.Result<"u123", never>, ab: (a: Result.Result<"u123", never>) => Result.Result<string, Error>, bc: (b: Result.Result<string, Error>) => Result.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>) => Result.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error> (+25 overloads)
pipe
(
import Result
Result
.
const succeed: <"u123">(value: "u123") => Result.Result<"u123", never> (+1 overload)
succeed
('u123'),
import Result
Result
.
const andThen: <Result.Result<"u123", never>, Result.Failure<Error> | Result.Success<string>>(fn: (a: "u123") => Result.Failure<Error> | Result.Success<string>) => (result: Result.Result<"u123", never>) => Result.Result<string, Error> (+1 overload)
andThen
(
const validateUser: (id: string) => Result.Failure<Error> | Result.Success<string>
validateUser
),
import Result
Result
.
const map: <Result.Result<string, Error>, {
    readonly id: string;
    readonly name: "John Doe";
}>(fn: (a: string) => {
    readonly id: string;
    readonly name: "John Doe";
}) => (result: Result.Result<string, Error>) => Result.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error> (+1 overload)
map
(
id: string
id
=> ({
id: string
id
,
name: "John Doe"
name
: 'John Doe' }))
); if (
import Result
Result
.
const isSuccess: <{
    readonly id: string;
    readonly name: "John Doe";
}>(result: Result.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, unknown>) => result is Result.Success<{
    readonly id: string;
    readonly name: "John Doe";
}>

Type guard to check if a Result is a Success .

@function@typeParamT - The type of the success value.@paramresult - The Result to check.@returnstrue if the result is a Success, otherwise false.@example
import { Result } from '@praha/byethrow';

const result: Result.Result<number, string> = { type: 'Success', value: 10 };
if (Result.isSuccess(result)) {
  console.log(result.value); // Safe access to value
}
@categoryType Guards
isSuccess
(
const result: Result.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>
result
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
//   Error: Whoops, something bad happened
//     at [eval]:5:15
//     at Script.runInThisContext (node:vm:132:18)
//     at Object.runInThisContext (node:vm:309:38)
//     at node:internal/process/execution:77:19
//     at [eval]-wrapper:6:22
//     at evalScript (node:internal/process/execution:76:60)
//     at node:internal/main/eval_string:23:3

const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);

myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err

const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
@seesource
console
.
Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0 .1.100
log
(
const result: Result.Success<{
    readonly id: string;
    readonly name: "John Doe";
}>
result
.
value: {
    readonly id: string;
    readonly name: "John Doe";
}
value
);
}

短縮エイリアス(R

簡潔さを重視するコードには、R エイリアスを使用します。

import { 
import R
R
} from '@praha/byethrow';
const
const validateUser: (id: string) => R.Failure<Error> | R.Success<string>
validateUser
= (
id: string
id
: string) => {
if (!
id: string
id
.
String.startsWith(searchString: string, position?: number): boolean

Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.

startsWith
('u')) {
return
import R
R
.
const fail: <Error>(error: Error) => R.Result<never, Error> (+1 overload)
fail
(new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('Invalid ID format'));
} return
import R
R
.
const succeed: <string>(value: string) => R.Result<string, never> (+1 overload)
succeed
(
id: string
id
);
}; const
const result: R.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>
result
=
import R
R
.
const pipe: <R.Result<"u123", never>, R.Result<string, Error>, R.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>>(a: R.Result<"u123", never>, ab: (a: R.Result<"u123", never>) => R.Result<string, Error>, bc: (b: R.Result<string, Error>) => R.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>) => R.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error> (+25 overloads)
pipe
(
import R
R
.
const succeed: <"u123">(value: "u123") => R.Result<"u123", never> (+1 overload)
succeed
('u123'),
import R
R
.
const andThen: <R.Result<"u123", never>, R.Failure<Error> | R.Success<string>>(fn: (a: "u123") => R.Failure<Error> | R.Success<string>) => (result: R.Result<"u123", never>) => R.Result<string, Error> (+1 overload)
andThen
(
const validateUser: (id: string) => R.Failure<Error> | R.Success<string>
validateUser
),
import R
R
.
const map: <R.Result<string, Error>, {
    readonly id: string;
    readonly name: "John Doe";
}>(fn: (a: string) => {
    readonly id: string;
    readonly name: "John Doe";
}) => (result: R.Result<string, Error>) => R.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error> (+1 overload)
map
(
id: string
id
=> ({
id: string
id
,
name: "John Doe"
name
: 'John Doe' }))
); if (
import R
R
.
const isSuccess: <{
    readonly id: string;
    readonly name: "John Doe";
}>(result: R.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, unknown>) => result is R.Success<{
    readonly id: string;
    readonly name: "John Doe";
}>

Type guard to check if a Result is a Success .

@function@typeParamT - The type of the success value.@paramresult - The Result to check.@returnstrue if the result is a Success, otherwise false.@example
import { Result } from '@praha/byethrow';

const result: Result.Result<number, string> = { type: 'Success', value: 10 };
if (Result.isSuccess(result)) {
  console.log(result.value); // Safe access to value
}
@categoryType Guards
isSuccess
(
const result: R.Result<{
    readonly id: string;
    readonly name: "John Doe";
}, Error>
result
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
//   Error: Whoops, something bad happened
//     at [eval]:5:15
//     at Script.runInThisContext (node:vm:132:18)
//     at Object.runInThisContext (node:vm:309:38)
//     at node:internal/process/execution:77:19
//     at [eval]-wrapper:6:22
//     at evalScript (node:internal/process/execution:76:60)
//     at node:internal/main/eval_string:23:3

const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);

myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err

const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
@seesource
console
.
Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0 .1.100
log
(
const result: R.Success<{
    readonly id: string;
    readonly name: "John Doe";
}>
result
.
value: {
    readonly id: string;
    readonly name: "John Doe";
}
value
);
}

Tree-Shakingサポート

@praha/byethrow完全なTree-shakingサポートを実現しています。

// 例:小規模アプリケーションでの使用
import { 
import R
R
} from '@praha/byethrow';
// 実際に使用されているのはこれらの機能のみ const
const parseNumber: (input: string) => R.Result<number, Error>
parseNumber
=
import R
R
.
fn<(input: string) => number, Error>(options: {
    try: (input: string) => number;
    catch: (error: unknown) => Error;
}): (input: string) => R.Result<number, Error> (+3 overloads)
export fn

Wraps a function that may throw and returns a new function that returns a Result or ResultAsync .

You can use either a custom catch handler or rely on the safe: true option to assume the function cannot throw.

@function@typeParamT - The function type to execute (sync or async) or a Promise type.@typeParamE - The error type to return if catch is used.@returnsA new function that returns a Result or ResultAsync wrapping the original function's return value or the caught error.@example

Sync try-catch

import { Result } from '@praha/byethrow';

const fn = Result.fn({
try: (x: number) => {
if (x < 0) throw new Error('Negative!');
return x * 2;
},
catch: (error) => new Error('Oops!', { cause: error }),
});

const result = fn(5); // Result.Result<number, Error>
@example

Sync safe

import { Result } from '@praha/byethrow';

const fn = Result.fn({
safe: true,
try: (x: number) => x + 1,
});

const result = fn(1); // Result.Result<number, never>
@example

Async try-catch

import { Result } from '@praha/byethrow';

const fn = Result.fn({
try: async (id: string) => await fetch(`/api/data/${id}`),
catch: (error) => new Error('Oops!', { cause: error }),
});

const result = await fn('abc'); // Result.ResultAsync<Response, Error>
@example

Async safe

import { Result } from '@praha/byethrow';

const fn = Result.fn({
safe: true,
try: async () => await Promise.resolve('ok'),
});

const result = await fn(); // Result.ResultAsync<string, never>
@categoryCreators
fn
({
try: (input: string) => number
try
: (
input: string
input
: string) =>
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

@paramstring A string to convert into a number.@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
parseInt
(
input: string
input
, 10),
catch: (error: unknown) => Error
catch
: () => new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('Invalid number')
}); // この場合、parseNumberに必要な最小限のコードのみが // バンドルに含まれ、他の機能(andThen、pipeなど)は除外されます

ベストプラクティス

インポート方法の選択

  • Result を使用する:各操作の目的を明確に示す、明示的で説明的な命名を好む場合
  • R を使用する:より少ないキーストロークと簡潔なコードで、より速い開発を好む場合

重要:インポート方法を混在させない

同じコードベース内で ResultR を混在させることは強くお勧めしません。 コードの可読性と一貫性を維持するために、プロジェクト全体で1つのアプローチを選択し、一貫して使用するように気をつけてください。

// @filename: mixed-imports.ts
// ❌ アプローチを混在させないでください - 一貫性のないコードになります
import { 
import Result
Result
,
import R
R
} from '@praha/byethrow';
const
const validateId: (id: string) => Result.Result<string, never>
validateId
= (
id: string
id
: string) => {
return
import Result
Result
.
const succeed: <string>(value: string) => Result.Result<string, never> (+1 overload)
succeed
(
id: string
id
); // Resultを使用
}; const
const processData: Result.Result<string, never>
processData
=
import R
R
.
const pipe: <Result.Result<"data", never>, Result.Result<string, never>>(a: Result.Result<"data", never>, ab: (a: Result.Result<"data", never>) => Result.Result<string, never>) => Result.Result<string, never> (+25 overloads)
pipe
( // Rを使用
import R
R
.
const succeed: <"data">(value: "data") => Result.Result<"data", never> (+1 overload)
succeed
('data'),
import R
R
.
const andThen: <Result.Result<"data", never>, Result.Result<string, never>>(fn: (a: "data") => Result.Result<string, never>) => (result: Result.Result<"data", never>) => Result.Result<string, never> (+1 overload)
andThen
(
const validateId: (id: string) => Result.Result<string, never>
validateId
)
); // @filename: consistent-imports.ts // ✅ 1つのアプローチを選択し、一貫して使用してください import {
import Result
Result
} from '@praha/byethrow';
const
const validateId: (id: string) => Result.Result<string, never>
validateId
= (
id: string
id
: string) => {
return
import Result
Result
.
const succeed: <string>(value: string) => Result.Result<string, never> (+1 overload)
succeed
(
id: string
id
);
}; const
const processData: Result.Result<string, never>
processData
=
import Result
Result
.
const pipe: <Result.Result<"data", never>, Result.Result<string, never>>(a: Result.Result<"data", never>, ab: (a: Result.Result<"data", never>) => Result.Result<string, never>) => Result.Result<string, never> (+25 overloads)
pipe
(
import Result
Result
.
const succeed: <"data">(value: "data") => Result.Result<"data", never> (+1 overload)
succeed
('data'),
import Result
Result
.
const andThen: <Result.Result<"data", never>, Result.Result<string, never>>(fn: (a: "data") => Result.Result<string, never>) => (result: Result.Result<"data", never>) => Result.Result<string, never> (+1 overload)
andThen
(
const validateId: (id: string) => Result.Result<string, never>
validateId
)
);