logo
byethrow
Guide
Examples
API Reference
Guide
Examples
API Reference
logo
byethrow
@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: combine()
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: succeed()
Function: try()
Function: unwrap()
Function: unwrapError()

Last Updated:

Previous PageType Alias: Failure<E>
Next PageType Alias: InferSuccess<T>

#Type Alias: InferFailure<T>

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

Defined in: result.ts:198

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