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: InferFailure<T>
Next PageType Alias: Result<T, E>

#Type Alias: InferSuccess<T>

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

Defined in: result.ts:170

Infers the Success value type T 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 SuccessValue = number
SuccessValue
=
import Result
Result
.
type InferSuccess<T> = [T] extends [(...args: any[]) => Result.ResultMaybeAsync<infer U, any>] ? U : [T] extends [Result.ResultMaybeAsync<infer U, any>] ? U : never

Infers the Success value type T 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 SuccessValue = Result.InferSuccess<R>; // number
@example

From a function

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

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

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.resolve<{
    readonly type: "Success";
    readonly value: 123;
}>(value: {
    readonly type: "Success";
    readonly value: 123;
}): Promise<{
    readonly type: "Success";
    readonly value: 123;
}> (+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: "Success"
type
: 'Success',
value: 123
value
: 123 } as
type const = {
    readonly type: "Success";
    readonly value: 123;
}
const
);
type
type SuccessValue = 123
SuccessValue
=
import Result
Result
.
type InferSuccess<T> = [T] extends [(...args: any[]) => Result.ResultMaybeAsync<infer U, any>] ? U : [T] extends [Result.ResultMaybeAsync<infer U, any>] ? U : never

Infers the Success value type T 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 SuccessValue = Result.InferSuccess<R>; // number
@example

From a function

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

const fn = () => Promise.resolve({ type: 'Success', value: 123 } as const);
type SuccessValue = Result.InferSuccess<typeof fn>; // number
@categoryInfer Types
InferSuccess
<typeof
const fn: () => Promise<{
    readonly type: "Success";
    readonly value: 123;
}>
fn
>; // number