• English
  • Function: unwrapError()

    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.

    Type Param

    R

    The input Result or ResultAsync.

    Type Param

    T

    The default value type (optional).

    Examples

    Failure Case (without default)

    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
    .
    const fail: <"Oops">(error: "Oops") => Result.Result<never, "Oops"> (+1 overload)
    fail
    ('Oops');
    const
    const error: string
    error
    =
    import Result
    Result
    .
    const unwrapError: <Result.Result<number, string>>(result: Result.Result<number, string>) => string (+3 overloads)
    unwrapError
    (
    const result: Result.Result<number, string>
    result
    ); // 'Oops'

    Failure Case (with default)

    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
    .
    const fail: <"Oops">(error: "Oops") => Result.Result<never, "Oops"> (+1 overload)
    fail
    ('Oops');
    const
    const error: string
    error
    =
    import Result
    Result
    .
    const unwrapError: <Result.Result<number, string>, "default">(result: Result.Result<number, string>, defaultValue: "default") => string (+3 overloads)
    unwrapError
    (
    const result: Result.Result<number, string>
    result
    , 'default'); // 'Oops'

    Success Case (without default)

    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
    .
    const succeed: <100>(value: 100) => Result.Result<100, never> (+1 overload)
    succeed
    (100);
    const
    const error: string
    error
    =
    import Result
    Result
    .
    const unwrapError: <Result.Result<number, string>>(result: Result.Result<number, string>) => string (+3 overloads)
    unwrapError
    (
    const result: Result.Result<number, string>
    result
    ); // throws 100

    Success Case (with default)

    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
    .
    const succeed: <100>(value: 100) => Result.Result<100, never> (+1 overload)
    succeed
    (100);
    const
    const error: string | 0
    error
    =
    import Result
    Result
    .
    const unwrapError: <Result.Result<number, string>, 0>(result: Result.Result<number, string>, defaultValue: 0) => string | 0 (+3 overloads)
    unwrapError
    (
    const result: Result.Result<number, string>
    result
    , 0); // 0

    See

    assertFailure - When used with assertFailure, you can safely unwrap the Result.

    Call Signature

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

    Type Parameters

    R

    R extends ResultMaybeAsync<any, any>

    Parameters

    result

    R

    Returns

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

    Call Signature

    unwrapError<R, T>(result, defaultValue): true extends HasPromise<R> ? Promise<T | InferFailure<R>> : T | InferFailure<R>

    Type Parameters

    R

    R extends ResultMaybeAsync<any, any>

    T

    T

    Parameters

    result

    R

    defaultValue

    T

    Returns

    true extends HasPromise<R> ? Promise<T | InferFailure<R>> : T | InferFailure<R>

    Call Signature

    unwrapError<R>(): (result) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>

    Type Parameters

    R

    R extends ResultMaybeAsync<any, any>

    Returns

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

    Call Signature

    unwrapError<R, T>(defaultValue): (result) => true extends HasPromise<R> ? Promise<T | InferFailure<R>> : T | InferFailure<R>

    Type Parameters

    R

    R extends ResultMaybeAsync<any, any>

    T

    T

    Parameters

    defaultValue

    T

    Returns

    (result) => true extends HasPromise<R> ? Promise<T | InferFailure<R>> : T | InferFailure<R>