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 pageType Alias: ResultAsync<T, E>Next pageType Alias: ResultMaybeAsync<T, E>

    #Type Alias: ResultFor<R, T, E>

    ResultFor<R, T, E> = true extends HasPromise<R> ? ResultAsync<T, E> : Result<T, E>

    Defined in: result.ts:145

    Resolves to the appropriate Result type (sync or async) based on the input type.

    Typically used to conditionally infer return types based on whether the original computation was async.

    #Type Parameters

    #R

    R

    The reference type to inspect for a Promise.

    #T

    T

    The type of the Success value.

    #E

    E

    The type of the Failure value.

    #Examples

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    type
    type R = Promise<Result.Result<number, string>>
    R
    =
    import Result
    Result
    .
    type ResultAsync<T, E> = Promise<Result.Result<T, E>>

    An asynchronous variant of Result , wrapped in a Promise.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const fetchData = async (): Result.ResultAsync<string, Error> => {
      try {
        const data = await fetch('...');
        return { type: 'Success', value: await data.text() };
      } catch (err) {
        return { type: 'Failure', error: err as Error };
      }
    };
    @categoryCore Types
    ResultAsync
    <number, string>;
    type
    type Output = Promise<Result.Result<number, string>>
    Output
    =
    import Result
    Result
    .
    type ResultFor<R, T, E> = true extends HasPromise<R> ? Result.ResultAsync<T, E> : Result.Result<T, E>

    Resolves to the appropriate Result type (sync or async) based on the input type.

    Typically used to conditionally infer return types based on whether the original computation was async.

    @typeParamR - The reference type to inspect for a Promise.@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example

    With a Promise-returning function

    import { Result } from '@praha/byethrow';
    
    type R = Result.ResultAsync<number, string>;
    type Output = Result.ResultFor<R, number, string>; // Result.ResultAsync<number, string>
    @example

    With a non-Promise-returning function

    import { Result } from '@praha/byethrow';
    
    type R = Result.Result<number, string>;
    type Output = Result.ResultFor<R, number, string>; // Result.Result<number, string>
    @categoryCore Types
    ResultFor
    <
    type R = Promise<Result.Result<number, string>>
    R
    , number, string>; // Result.ResultAsync<number, string>
    import { 
    import Result
    Result
    } from '@praha/byethrow';
    type
    type R = Result.Success<number> | Result.Failure<string>
    R
    =
    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>;
    type
    type Output = Result.Success<number> | Result.Failure<string>
    Output
    =
    import Result
    Result
    .
    type ResultFor<R, T, E> = true extends HasPromise<R> ? Result.ResultAsync<T, E> : Result.Result<T, E>

    Resolves to the appropriate Result type (sync or async) based on the input type.

    Typically used to conditionally infer return types based on whether the original computation was async.

    @typeParamR - The reference type to inspect for a Promise.@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example

    With a Promise-returning function

    import { Result } from '@praha/byethrow';
    
    type R = Result.ResultAsync<number, string>;
    type Output = Result.ResultFor<R, number, string>; // Result.ResultAsync<number, string>
    @example

    With a non-Promise-returning function

    import { Result } from '@praha/byethrow';
    
    type R = Result.Result<number, string>;
    type Output = Result.ResultFor<R, number, string>; // Result.Result<number, string>
    @categoryCore Types
    ResultFor
    <
    type R = Result.Success<number> | Result.Failure<string>
    R
    , number, string>; // Result.Result<number, string>