Type Alias: ResultFor<R, T, E>

ResultFor<R, T, E> = true extends HasPromise<R> ? ResultAsync<T, E> : 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.

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>