For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt, and this page is available as Markdown at /byethrow/guide/tutorial/resolving/asserting.md.
  • English
  • Asserting Results

    After you have handled every possible error, for example with orElse, the error type of a Result becomes never. assertSuccess and assertFailure let you state this in your code, and TypeScript verifies it at compile time.

    Asserting Success with assertSuccess

    assertSuccess takes a Result whose error type is never and returns it as a 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 }

    When Errors Remain

    If the error type is not never, meaning some errors might still occur, TypeScript reports a compile error:

    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);
    // ❌ The error type is string, not never 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'.

    Handling Every Error with orElse

    Use orElse to handle the errors so that the error type becomes 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)), // The error type is now 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 }

    If you later add a new error to getResult and forget to handle it, assertSuccess fails to compile, pointing you to the place that needs updating.

    Asserting Failure with assertFailure

    assertFailure is the counterpart of assertSuccess. It takes a Result whose success type is never and returns it as a 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' }

    If the success type is not never, TypeScript reports a compile error:

    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');
    // ❌ The success type is number, not never 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'.

    Unwrapping Safely with assertSuccess and unwrap

    unwrap throws when it receives a Failure. By calling assertSuccess first, you guarantee at compile time that there is no Failure left, so unwrap cannot 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)), // Handle every error
    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
    , // Verify that no error remains
    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
    (), // Cannot throw
    ); // value: number

    This pattern is especially useful at the boundaries of your application, where you want to extract the final value safely.

    Compared to Type Guards

    assertSuccess / assertFailure and isSuccess / isFailure serve different purposes:

    FunctionWhen the check happensPurpose
    isSuccess / isFailureRuntimeBranch on whether a Result is a success or failure
    assertSuccess / assertFailureCompile timeDeclare that only one variant is possible

    As a safeguard, assertSuccess and assertFailure also throw an error at runtime if they receive the unexpected variant, for example because of an incorrect type assertion elsewhere.

    References

    APIPurpose
    assertSuccess(result)Assert that a Result is a Success (the error type must be never)
    assertFailure(result)Assert that a Result is a Failure (the success type must be never)