For AI agents: the complete documentation index is available at /byethrow/ja/llms.txt, the full documentation bundle is available at /byethrow/ja/llms-full.txt, and this page is available as Markdown at /byethrow/ja/guide/tutorial/resolving/unwrapping.md.
  • 日本語
  • Result から値を取り出す

    どこかの時点で、Result から値を取り出し、Web フレームワークや CLI ランナーなど Result を知らないコードに渡す必要があります。 unwrap と unwrapError は、Result の中身をプレーンな値として取り出します。

    unwrap で値を取り出す

    unwrap(result, defaultValue?)ケースを選ぶと、その流れだけを追えます

    unwrap は Result の成功値を返します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: 42
    value
    =
    import Result
    Result
    .
    function unwrap<Result.Result<42, never>>(result: Result.Result<42, never>): 42 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (42));
    // value: 42

    Result が Failure の場合、unwrap はそのエラーを throw します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: never
    value
    =
    import Result
    Result
    .
    function unwrap<Result.Result<never, Error>>(result: Result.Result<never, Error>): never (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function fail<Error>(error: Error): Result.Result<never, Error> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    (new
    var Error: ErrorConstructor
    new (message?: string, options?: ErrorOptions) => Error (+1 overload)
    Error
    ('Something went wrong')));
    // 例外: Error('Something went wrong')

    デフォルト値を指定する

    第 2 引数にデフォルト値を渡すと、throw する代わりにその値を返します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const fallback: 0
    fallback
    =
    import Result
    Result
    .
    function unwrap<Result.Result<never, "error">, 0>(result: Result.Result<never, "error">, defaultValue: 0): 0 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function fail<"error">(error: "error"): Result.Result<never, "error"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('error'), 0);
    // fallback: 0 const
    const value: 0 | 42
    value
    =
    import Result
    Result
    .
    function unwrap<Result.Result<42, never>, 0>(result: Result.Result<42, never>, defaultValue: 0): 0 | 42 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (42), 0);
    // value: 42(デフォルト値は無視される)

    unwrapError でエラーを取り出す

    unwrapError(result, defaultValue?)ケースを選ぶと、その流れだけを追えます

    unwrapError は unwrap の対になる関数で、Result のエラー値を返します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const error: "Something went wrong"
    error
    =
    import Result
    Result
    .
    function unwrapError<Result.Result<never, "Something went wrong">>(result: Result.Result<never, "Something went wrong">): "Something went wrong" (+3 overloads)

    Extracts the error value from a Result or ResultAsync .

    If the input is a Success , it will throw the success value or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Failure Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result); // 'Oops'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result, 'default'); // 'Oops'
    @example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result); // throws 100
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result, 0); // 0
    @seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps
    unwrapError
    (
    import Result
    Result
    .
    function fail<"Something went wrong">(error: "Something went wrong"): Result.Result<never, "Something went wrong"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('Something went wrong'));
    // error: 'Something went wrong'

    Result が Success の場合、デフォルト値が指定されていなければ、unwrapError はその値を throw します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const error: never
    error
    =
    import Result
    Result
    .
    function unwrapError<Result.Result<42, never>>(result: Result.Result<42, never>): never (+3 overloads)

    Extracts the error value from a Result or ResultAsync .

    If the input is a Success , it will throw the success value or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Failure Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result); // 'Oops'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result, 'default'); // 'Oops'
    @example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result); // throws 100
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result, 0); // 0
    @seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps
    unwrapError
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (42));
    // 例外: 42 const
    const fallback: "No error"
    fallback
    =
    import Result
    Result
    .
    function unwrapError<Result.Result<42, never>, "No error">(result: Result.Result<42, never>, defaultValue: "No error"): "No error" (+3 overloads)

    Extracts the error value from a Result or ResultAsync .

    If the input is a Success , it will throw the success value or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Failure Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result); // 'Oops'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result, 'default'); // 'Oops'
    @example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result); // throws 100
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result, 0); // 0
    @seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps
    unwrapError
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (42), 'No error');
    // fallback: 'No error'

    パイプラインの中で値を取り出す

    Result を渡さずに呼び出すと、どちらの関数も引数の Result から値を取り出す関数を返します。 これにより、パイプラインの最後のステップとして使えます。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: number
    value
    =
    import Result
    Result
    .
    function pipe<Result.Result<21, never>, Result.Result<number, never>, number>(a: Result.Result<21, never>, ab: (a: Result.Result<21, never>) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => number): number (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<21>(value: 21): Result.Result<21, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (21),
    import Result
    Result
    .
    function map<Result.Result<21, never>, number>(fn: (a: 21) => number): (result: Result.Result<21, never>) => Result.Result<number, never> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    x: 21
    x
    ) =>
    x: 21
    x
    * 2),
    import Result
    Result
    .
    function unwrap<Result.Result<number, never>>(): (result: Result.Result<number, never>) => number (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (), // または Result.unwrap(defaultValue)
    ); // value: 42

    非同期の Result

    どちらの関数も ResultAsync を受け取り、プレーンな値の Promise を返します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: 42
    value
    = await
    import Result
    Result
    .
    function unwrap<Promise<Result.Result<42, never>>>(result: Promise<Result.Result<42, never>>): Promise<42> (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    var Promise: PromiseConstructor

    Represents the completion of an asynchronous operation

    Promise
    .
    PromiseConstructor.resolve<Result.Result<42, never>>(value: Result.Result<42, never>): Promise<Result.Result<42, never>> (+2 overloads)

    Creates a new resolved promise for the provided value.

    @paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
    resolve
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (42)));
    // value: 42 const
    const fallback: 0
    fallback
    = await
    import Result
    Result
    .
    function unwrap<Promise<Result.Result<never, "error">>, 0>(result: Promise<Result.Result<never, "error">>, defaultValue: 0): Promise<0> (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    var Promise: PromiseConstructor

    Represents the completion of an asynchronous operation

    Promise
    .
    PromiseConstructor.resolve<Result.Result<never, "error">>(value: Result.Result<never, "error">): Promise<Result.Result<never, "error">> (+2 overloads)

    Creates a new resolved promise for the provided value.

    @paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
    resolve
    (
    import Result
    Result
    .
    function fail<"error">(error: "error"): Result.Result<never, "error"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('error')), 0);
    // fallback: 0

    どこで値を取り出すべきか

    アプリケーションの境界で

    値の取り出しは、HTTP ハンドラーや CLI のエントリーポイントなど、プレーンな値を外部に渡さなければならないアプリケーションの境界で行いましょう。 想定しているエラーを先に処理し、残ったものだけを unwrap に throw させます。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const handleRequest: (req: Request) => Promise<Response>
    handleRequest
    = async (
    req: Request
    req
    : Request):
    interface Promise<T>

    Represents the completion of an asynchronous operation

    Promise
    <Response> => {
    return await
    import Result
    Result
    .
    function pipe<Result.ResultAsync<number, "NotFound" | "Unexpected">, Result.ResultAsync<Response, "NotFound" | "Unexpected">, Result.ResultAsync<Response, "Unexpected">, Promise<Response>>(a: Result.ResultAsync<number, "NotFound" | "Unexpected">, ab: (a: Result.ResultAsync<number, "NotFound" | "Unexpected">) => Result.ResultAsync<Response, "NotFound" | "Unexpected">, bc: (b: Result.ResultAsync<Response, "NotFound" | "Unexpected">) => Result.ResultAsync<...>, cd: (c: Result.ResultAsync<...>) => Promise<...>): Promise<...> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    const processRequest: (req: Request) => Result.ResultAsync<number, "NotFound" | "Unexpected">
    processRequest
    (
    req: Request
    req
    ),
    import Result
    Result
    .
    function map<Result.ResultAsync<number, "NotFound" | "Unexpected">, Response>(fn: (a: number) => Response): (result: Result.ResultAsync<number, "NotFound" | "Unexpected">) => Result.ResultAsync<Response, "NotFound" | "Unexpected"> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    result: number
    result
    ) => {
    return new
    var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response

    The Response interface of the Fetch API represents the response to a request.

    MDN Reference

    Response
    (
    var JSON: JSON

    An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

    JSON
    .
    JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

    Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

    @paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
    stringify
    (
    result: number
    result
    ), {
    ResponseInit.status?: number | undefined
    status
    : 200 });
    }),
    import Result
    Result
    .
    function orElse<Result.ResultAsync<Response, "NotFound" | "Unexpected">, Result.Success<Response> | Result.Failure<"Unexpected">>(fn: (a: "NotFound" | "Unexpected") => Result.Success<Response> | Result.Failure<"Unexpected">): (result: Result.ResultAsync<Response, "NotFound" | "Unexpected">) => Result.ResultAsync<Response, "Unexpected"> (+1 overload)

    Chains the next computation using the error value of a Result or ResultAsync . If the original result is a Success , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

    @typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(42),
    Result.orElse((error) => Result.succeed(0)),
    );
    // { type: 'Success', value: 42 }
    @example

    Failure Case (function returns a Success)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('original error'),
    Result.orElse((error) => Result.succeed('default value')),
    );
    // result: { type: 'Success', value: 'default value' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('original error'),
    Result.orElse((error) => Result.fail('new error: ' + error)),
    );
    // result: { type: 'Failure', error: 'new error: original error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    orElse
    ((
    error: "NotFound" | "Unexpected"
    error
    ) => {
    switch (
    error: "NotFound" | "Unexpected"
    error
    ) {
    case 'NotFound': return
    import Result
    Result
    .
    function succeed<Response>(value: Response): Result.Result<Response, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (new
    var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response

    The Response interface of the Fetch API represents the response to a request.

    MDN Reference

    Response
    ('Not Found', {
    ResponseInit.status?: number | undefined
    status
    : 404 }));
    default: return
    import Result
    Result
    .
    function fail<"Unexpected">(error: "Unexpected"): Result.Result<never, "Unexpected"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    (
    error: "Unexpected"
    error
    );
    } }), // 残ったエラーはここで throw され、Sentry や CloudWatch などの // インフラレベルのエラー監視で検知できる
    import Result
    Result
    .
    function unwrap<Result.ResultAsync<Response, "Unexpected">>(): (result: Result.ResultAsync<Response, "Unexpected">) => Promise<Response> (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (),
    ); };

    unwrap が決して throw しないことをコンパイル時に保証するには、次のページで紹介する assertSuccess と組み合わせてください。

    ロジックの途中では取り出さない

    ロジックの途中で値を取り出すのは避けましょう。 そうすると Result の世界から早々に抜け出すことになり、想定内の失敗が再び例外に戻ってしまいます。 代わりに値を Result に包んだまま、map、andThen、orElse などの関数で変換してください。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // ❌ こうしない const
    const bad: number
    bad
    =
    import Result
    Result
    .
    function unwrap<Result.Result<42, never>>(result: Result.Result<42, never>): 42 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (42)) * 2;
    // ✅ 代わりにこうする const
    const good: Result.Result<number, never>
    good
    =
    import Result
    Result
    .
    function pipe<Result.Result<42, never>, Result.Result<number, never>>(a: Result.Result<42, never>, ab: (a: Result.Result<42, never>) => Result.Result<number, never>): Result.Result<number, never> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (42),
    import Result
    Result
    .
    function map<Result.Result<42, never>, number>(fn: (a: 42) => number): (result: Result.Result<42, never>) => Result.Result<number, never> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    x: 42
    x
    ) =>
    x: 42
    x
    * 2),
    );

    リファレンス

    API用途
    unwrap(result)成功値を取り出す。失敗時は throw するかデフォルト値を返す
    unwrapError(result)エラー値を取り出す。成功時は throw するかデフォルト値を返す