logo
byethrow
  • Guide
  • Examples
  • API Reference
    @praha/byethrow
    Modules
    Result
    Types
    Type Alias: Failure<E>
    Type Alias: InferFailure<T>
    Type Alias: InferSuccess<T>
    Type Alias: Result<T, E>
    Type Alias: ResultAsync<T, E>
    Type Alias: ResultFor<R, T, E>
    Type Alias: ResultMaybeAsync<T, E>
    Type Alias: Success<T>
    Functions
    Function: andThen()
    Function: andThrough()
    Function: assertFailure()
    Function: assertSuccess()
    Function: bind()
    Function: collect()
    Function: do()
    Function: fail()
    Function: inspect()
    Function: inspectError()
    Function: isFailure()
    Function: isResult()
    Function: isSuccess()
    Function: map()
    Function: mapError()
    Function: orElse()
    Function: parse()
    Function: pipe()
    Function: sequence()
    Function: succeed()
    Function: try()
    Function: unwrap()
    Function: unwrapError()
    📝 Edit this page

    Last Updated:

    Previous pageFunction: unwrap()

    #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

    The input Result or ResultAsync.

    #Type Param

    The default value type (optional).

    #Examples

    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'
    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'
    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
    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>

    Defined in: functions/unwrap-error.ts:56

    #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>

    Defined in: functions/unwrap-error.ts:57

    #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>

    Defined in: functions/unwrap-error.ts:58

    #Type Parameters

    #R

    R extends ResultMaybeAsync<any, any>

    #Returns

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

    #Parameters

    #result

    R

    #Returns

    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>

    Defined in: functions/unwrap-error.ts:59

    #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>

    #Parameters

    #result

    R

    #Returns

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