Type Alias: InferFailure<T>

InferFailure<T> = [T] extends [(...args) => ResultMaybeAsync<any, infer U>] ? U : [T] extends [ResultMaybeAsync<any, infer U>] ? U : never

Infers the Failure value type E from a Result or a function returning a Result.

Type Parameters

T

T

A ResultMaybeAsync type or a function returning it.

Examples

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 ErrorValue = string
ErrorValue
=
import Result
Result
.
type InferFailure<T> = [T] extends [(...args: any[]) => Result.ResultMaybeAsync<any, infer U>] ? U : [T] extends [Result.ResultMaybeAsync<any, infer U>] ? U : never

Infers the Failure value type E from a Result or a function returning a Result.

@typeParamT - A ResultMaybeAsync type or a function returning it.@example

From a result object

import { Result } from '@praha/byethrow';

type R = Result.Result<number, string>;
type ErrorValue = Result.InferFailure<R>; // string
@example

From a function

import { Result } from '@praha/byethrow';

const fn = () => Promise.resolve({ type: 'Failure', error: new Error() } as const);
type ErrorValue = Result.InferFailure<typeof fn>; // Error
@categoryInfer Types
InferFailure
<
type R = Result.Success<number> | Result.Failure<string>
R
>; // string
import { 
import Result
Result
} from '@praha/byethrow';
const
const fn: () => Promise<{
    readonly type: "Failure";
    readonly error: Error;
}>
fn
= () =>
var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.resolve<{
    readonly type: "Failure";
    readonly error: Error;
}>(value: {
    readonly type: "Failure";
    readonly error: Error;
}): Promise<{
    readonly type: "Failure";
    readonly error: Error;
}> (+2 overloads)

Creates a new resolved promise for the provided value.

@paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
resolve
({
type: "Failure"
type
: 'Failure',
error: Error
error
: new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
() } as
type const = {
    readonly type: "Failure";
    readonly error: Error;
}
const
);
type
type ErrorValue = Error
ErrorValue
=
import Result
Result
.
type InferFailure<T> = [T] extends [(...args: any[]) => Result.ResultMaybeAsync<any, infer U>] ? U : [T] extends [Result.ResultMaybeAsync<any, infer U>] ? U : never

Infers the Failure value type E from a Result or a function returning a Result.

@typeParamT - A ResultMaybeAsync type or a function returning it.@example

From a result object

import { Result } from '@praha/byethrow';

type R = Result.Result<number, string>;
type ErrorValue = Result.InferFailure<R>; // string
@example

From a function

import { Result } from '@praha/byethrow';

const fn = () => Promise.resolve({ type: 'Failure', error: new Error() } as const);
type ErrorValue = Result.InferFailure<typeof fn>; // Error
@categoryInfer Types
InferFailure
<typeof
const fn: () => Promise<{
    readonly type: "Failure";
    readonly error: Error;
}>
fn
>; // Error