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/api/functions/Result.assertFailure.md.
  • 日本語
  • Function: assertFailure()

    assertFailure<R>(result): true extends HasPromise<R> ? Promise<Failure<InferFailure<R>>> : Failure<InferFailure<R>>

    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.

    Type Parameters

    R

    R extends ResultMaybeAsync<never, any>

    The result type that extends ResultMaybeAsync with never as the success type.

    Parameters

    result

    R

    The Result or ResultAsync to assert as a Failure. The success type must be never.

    Returns

    true extends HasPromise<R> ? Promise<Failure<InferFailure<R>>> : Failure<InferFailure<R>>

    The Failure result or a Promise of Failure result.

    Throws

    If the result is a Success at runtime.

    Examples

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<never, "error">
    result
    =
    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<"error">
    failure
    =
    import Result
    Result
    .
    function assertFailure<Result.Result<never, "error">>(result: Result.Result<never, "error">): Result.Failure<"error">

    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, "error">
    result
    );
    // failure: { type: 'Failure', error: 'error' }

    Type-safe usage after narrowing success type

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const getResult: () => Result.Result<number, string>
    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, 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 value: string
    value
    =
    import Result
    Result
    .
    function pipe<Result.Result<number, string>, Result.Result<never, string>, Result.Failure<string>, string>(a: Result.Result<number, string>, ab: (a: Result.Result<number, string>) => Result.Result<never, string>, bc: (b: Result.Result<never, string>) => Result.Failure<string>, cd: (c: Result.Failure<string>) => string): string (+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, string>
    getResult
    (),
    import Result
    Result
    .
    function andThen<Result.Result<number, string>, Result.Result<never, "die">>(fn: (a: number) => Result.Result<never, "die">): (result: Result.Result<number, string>) => Result.Result<never, string> (+1 overload)

    Chains the next computation using the success value of a Result or ResultAsync . If the original result is a Failure , 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(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    (() =>
    import Result
    Result
    .
    function fail<"die">(error: "die"): Result.Result<never, "die"> (+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
    ('die')), // Success type becomes never
    import Result
    Result
    .
    function assertFailure<R extends Result.ResultMaybeAsync<never, any>>(result: R): true extends HasPromise<R> ? Promise<Result.Failure<Result.InferFailure<R>>> : Result.Failure<Result.InferFailure<R>>

    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
    , // Type-safe: success type is now never
    import Result
    Result
    .
    function unwrapError<Result.Failure<string>>(): (result: Result.Failure<string>) => string (+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
    (), // Safe unwrap after assertion
    );

    See

    unwrapError - Use with assertFailure to safely unwrap error values.