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/asserting.md.
  • 日本語
  • Result をアサートする

    orElse などですべてのエラーを処理し終えると、Result のエラー型は never になります。 assertSuccess と assertFailure を使うと、このことをコードで明示でき、TypeScript がそれをコンパイル時に検証します。

    assertSuccess で成功をアサートする

    assertSuccess は、エラー型が never の Result を受け取り、それを Success として返します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, never>
    result
    :
    import Result
    Result
    .
    type Result<T, E> = Result.Success<T> | Result.Failure<E>

    A union type representing either a success or a failure.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const doSomething = (): Result.Result<number, string> => {
      return Math.random() > 0.5
        ? { type: 'Success', value: 10 }
        : { type: 'Failure', error: 'Oops' };
    };
    @categoryCore Types
    Result
    <number, never> =
    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);
    const
    const success: Result.Success<number>
    success
    =
    import Result
    Result
    .
    function assertSuccess<Result.Result<number, never>>(result: Result.Result<number, never>): Result.Success<number>

    Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

    @typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const success = Result.assertSuccess(result);
    // success: { type: 'Success', value: 42 }
    @example

    Type-safe usage after narrowing error type

    import { Result } from '@praha/byethrow';
    
    const getResult = (): Result.Result<number, string> => Result.succeed(42);
    const value = Result.pipe(
    getResult(),
    Result.orElse(() => Result.succeed(0)), // Error type becomes never
    Result.assertSuccess, // Type-safe: error type is now never
    Result.unwrap(), // Safe unwrap after assertion
    );
    @seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
    assertSuccess
    (
    const result: Result.Result<number, never>
    result
    );
    // success: { type: 'Success', value: 42 }

    エラーが残っている場合

    エラー型が never でない、つまりまだエラーが起こりうる場合、TypeScript はコンパイルエラーを報告します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, string>
    result
    :
    import Result
    Result
    .
    type Result<T, E> = Result.Success<T> | Result.Failure<E>

    A union type representing either a success or a failure.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const doSomething = (): Result.Result<number, string> => {
      return Math.random() > 0.5
        ? { type: 'Success', value: 10 }
        : { type: 'Failure', error: 'Oops' };
    };
    @categoryCore Types
    Result
    <number, string> =
    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);
    // ❌ エラー型が never ではなく string になっている const
    const success: Promise<Result.Success<any>>
    success
    =
    import Result
    Result
    .
    function assertSuccess<Result.ResultMaybeAsync<any, never>>(result: Result.ResultMaybeAsync<any, never>): Promise<Result.Success<any>>

    Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

    @typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const success = Result.assertSuccess(result);
    // success: { type: 'Success', value: 42 }
    @example

    Type-safe usage after narrowing error type

    import { Result } from '@praha/byethrow';
    
    const getResult = (): Result.Result<number, string> => Result.succeed(42);
    const value = Result.pipe(
    getResult(),
    Result.orElse(() => Result.succeed(0)), // Error type becomes never
    Result.assertSuccess, // Type-safe: error type is now never
    Result.unwrap(), // Safe unwrap after assertion
    );
    @seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
    assertSuccess
    (result);
    Argument of type 'Result<number, string>' is not assignable to parameter of type 'ResultMaybeAsync<any, never>'. Type 'Failure<string>' is not assignable to type 'ResultMaybeAsync<any, never>'. Type 'Failure<string>' is not assignable to type 'Failure<never> | Promise<Result<any, never>>'. Type 'Failure<string>' is not assignable to type 'Failure<never>'. Type 'string' is not assignable to type 'never'.

    orElse ですべてのエラーを処理する

    orElse でエラーを処理して、エラー型を never にしましょう。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    declare const
    const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
    getResult
    : () =>
    import Result
    Result
    .
    type Result<T, E> = Result.Success<T> | Result.Failure<E>

    A union type representing either a success or a failure.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const doSomething = (): Result.Result<number, string> => {
      return Math.random() > 0.5
        ? { type: 'Success', value: 10 }
        : { type: 'Failure', error: 'Oops' };
    };
    @categoryCore Types
    Result
    <number, 'NotFound' | 'NetworkError'>;
    const
    const result: Result.Success<number>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<number, "NotFound" | "NetworkError">, Result.Result<number, never>, Result.Success<number>>(a: Result.Result<number, "NotFound" | "NetworkError">, ab: (a: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => Result.Success<number>): Result.Success<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
    (
    const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
    getResult
    (),
    import Result
    Result
    .
    function orElse<Result.Result<number, "NotFound" | "NetworkError">, Result.Result<0, never>>(fn: (a: "NotFound" | "NetworkError") => Result.Result<0, never>): (result: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never> (+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
    (() =>
    import Result
    Result
    .
    function succeed<0>(value: 0): Result.Result<0, 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
    (0)), // エラー型は never になる
    import Result
    Result
    .
    function assertSuccess<R extends Result.ResultMaybeAsync<any, never>>(result: R): true extends HasPromise<R> ? Promise<Result.Success<Result.InferSuccess<R>>> : Result.Success<Result.InferSuccess<R>>

    Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

    @typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const success = Result.assertSuccess(result);
    // success: { type: 'Success', value: 42 }
    @example

    Type-safe usage after narrowing error type

    import { Result } from '@praha/byethrow';
    
    const getResult = (): Result.Result<number, string> => Result.succeed(42);
    const value = Result.pipe(
    getResult(),
    Result.orElse(() => Result.succeed(0)), // Error type becomes never
    Result.assertSuccess, // Type-safe: error type is now never
    Result.unwrap(), // Safe unwrap after assertion
    );
    @seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
    assertSuccess
    ,
    ); // result: { type: 'Success', value: number }

    後から getResult に新しいエラーを追加して処理し忘れた場合、assertSuccess がコンパイルエラーになり、修正が必要な箇所を教えてくれます。

    assertFailure で失敗をアサートする

    assertFailure は assertSuccess の対になる関数です。 成功型が never の Result を受け取り、それを Failure として返します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<never, string>
    result
    :
    import Result
    Result
    .
    type Result<T, E> = Result.Success<T> | Result.Failure<E>

    A union type representing either a success or a failure.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const doSomething = (): Result.Result<number, string> => {
      return Math.random() > 0.5
        ? { type: 'Success', value: 10 }
        : { type: 'Failure', error: 'Oops' };
    };
    @categoryCore Types
    Result
    <never, string> =
    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');
    const
    const failure: Result.Failure<string>
    failure
    =
    import Result
    Result
    .
    function assertFailure<Result.Result<never, string>>(result: Result.Result<never, string>): Result.Failure<string>

    Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

    @typeParamR - The result type that extends ResultMaybeAsync with never as the success type.@paramresult - The Result or ResultAsync to assert as a Failure. The success type must be never.@returnsThe Failure result or a Promise of Failure result.@throws{Error} If the result is a Success at runtime.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const failure = Result.assertFailure(result);
    // failure: { type: 'Failure', error: 'error' }
    @example

    Type-safe usage after narrowing success type

    import { Result } from '@praha/byethrow';
    
    const getResult = (): Result.Result<number, string> => Result.fail('error');
    const value = Result.pipe(
    getResult(),
    Result.andThen(() => Result.fail('die')), // Success type becomes never
    Result.assertFailure, // Type-safe: success type is now never
    Result.unwrapError(), // Safe unwrap after assertion
    );
    @seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
    assertFailure
    (
    const result: Result.Result<never, string>
    result
    );
    // failure: { type: 'Failure', error: 'error' }

    成功型が never でない場合、TypeScript はコンパイルエラーを報告します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, string>
    result
    :
    import Result
    Result
    .
    type Result<T, E> = Result.Success<T> | Result.Failure<E>

    A union type representing either a success or a failure.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const doSomething = (): Result.Result<number, string> => {
      return Math.random() > 0.5
        ? { type: 'Success', value: 10 }
        : { type: 'Failure', error: 'Oops' };
    };
    @categoryCore Types
    Result
    <number, string> =
    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');
    // ❌ 成功型が never ではなく number になっている const
    const failure: Promise<Result.Failure<any>>
    failure
    =
    import Result
    Result
    .
    function assertFailure<Result.ResultMaybeAsync<never, any>>(result: Result.ResultMaybeAsync<never, any>): Promise<Result.Failure<any>>

    Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

    @typeParamR - The result type that extends ResultMaybeAsync with never as the success type.@paramresult - The Result or ResultAsync to assert as a Failure. The success type must be never.@returnsThe Failure result or a Promise of Failure result.@throws{Error} If the result is a Success at runtime.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const failure = Result.assertFailure(result);
    // failure: { type: 'Failure', error: 'error' }
    @example

    Type-safe usage after narrowing success type

    import { Result } from '@praha/byethrow';
    
    const getResult = (): Result.Result<number, string> => Result.fail('error');
    const value = Result.pipe(
    getResult(),
    Result.andThen(() => Result.fail('die')), // Success type becomes never
    Result.assertFailure, // Type-safe: success type is now never
    Result.unwrapError(), // Safe unwrap after assertion
    );
    @seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
    assertFailure
    (result);
    Argument of type 'Result<number, string>' is not assignable to parameter of type 'ResultMaybeAsync<never, any>'. Type 'Success<number>' is not assignable to type 'ResultMaybeAsync<never, any>'. Type 'Success<number>' is not assignable to type 'Success<never> | Promise<Result<never, any>>'. Type 'Success<number>' is not assignable to type 'Success<never>'. Type 'number' is not assignable to type 'never'.

    assertSuccess と unwrap で安全に値を取り出す

    unwrap は Failure を受け取ると throw します。 先に assertSuccess を呼び出しておけば、Failure が残っていないことがコンパイル時に保証されるため、unwrap が throw することはありません。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    declare const
    const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
    getResult
    : () =>
    import Result
    Result
    .
    type Result<T, E> = Result.Success<T> | Result.Failure<E>

    A union type representing either a success or a failure.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const doSomething = (): Result.Result<number, string> => {
      return Math.random() > 0.5
        ? { type: 'Success', value: 10 }
        : { type: 'Failure', error: 'Oops' };
    };
    @categoryCore Types
    Result
    <number, 'NotFound' | 'NetworkError'>;
    const
    const value: number
    value
    =
    import Result
    Result
    .
    function pipe<Result.Result<number, "NotFound" | "NetworkError">, Result.Result<number, never>, Result.Success<number>, number>(a: Result.Result<number, "NotFound" | "NetworkError">, ab: (a: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => Result.Success<number>, cd: (c: Result.Success<number>) => 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
    (
    const getResult: () => Result.Result<number, "NotFound" | "NetworkError">
    getResult
    (),
    import Result
    Result
    .
    function orElse<Result.Result<number, "NotFound" | "NetworkError">, Result.Result<0, never>>(fn: (a: "NotFound" | "NetworkError") => Result.Result<0, never>): (result: Result.Result<number, "NotFound" | "NetworkError">) => Result.Result<number, never> (+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
    (() =>
    import Result
    Result
    .
    function succeed<0>(value: 0): Result.Result<0, 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
    (0)), // すべてのエラーを処理する
    import Result
    Result
    .
    function assertSuccess<R extends Result.ResultMaybeAsync<any, never>>(result: R): true extends HasPromise<R> ? Promise<Result.Success<Result.InferSuccess<R>>> : Result.Success<Result.InferSuccess<R>>

    Asserts that a Result or ResultAsync is a Success and returns it. This function requires that the result's error type is never, meaning the result is guaranteed to be a Success at the type level. If the result is a Failure at runtime, throws an error.

    @typeParamR - The result type that extends ResultMaybeAsync with never as the error type.@paramresult - The Result or ResultAsync to assert as a Success. The error type must be never.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure at runtime.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const success = Result.assertSuccess(result);
    // success: { type: 'Success', value: 42 }
    @example

    Type-safe usage after narrowing error type

    import { Result } from '@praha/byethrow';
    
    const getResult = (): Result.Result<number, string> => Result.succeed(42);
    const value = Result.pipe(
    getResult(),
    Result.orElse(() => Result.succeed(0)), // Error type becomes never
    Result.assertSuccess, // Type-safe: error type is now never
    Result.unwrap(), // Safe unwrap after assertion
    );
    @seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
    assertSuccess
    , // エラーが残っていないことを検証する
    import Result
    Result
    .
    function unwrap<Result.Success<number>>(): (result: Result.Success<number>) => 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
    (), // throw しない
    ); // value: number

    このパターンは、最終的な値を安全に取り出したいアプリケーションの境界で特に役立ちます。

    型ガードとの比較

    assertSuccess / assertFailure と isSuccess / isFailure は目的が異なります。

    関数チェックのタイミング用途
    isSuccess / isFailure実行時Result が成功か失敗かで処理を分岐する
    assertSuccess / assertFailureコンパイル時一方のバリアントしかありえないことを宣言する

    安全策として、assertSuccess と assertFailure は、どこか別の場所での誤った型アサーションなどが原因で想定外のバリアントを受け取った場合、実行時にもエラーを throw します。

    リファレンス

    API用途
    assertSuccess(result)Result が Success であることをアサートする(エラー型は never である必要がある)
    assertFailure(result)Result が Failure であることをアサートする(成功型は never である必要がある)