Skip to content

Class.Result

GitHub Actions edited this page Oct 24, 2024 · 23 revisions

resultar / Result

Class: Result<T, E>

A Result is a type that represents either success (Ok) or failure (Err). It is often used to replace exceptions or null returns. Result is often used to handle errors in a functional way. The Result type is often used to handle errors in a functional way.

Type Parameters

T

E

Implements

  • Resultable<T, E>

Properties

error

readonly error: E

Implementation of

Resultable.error

Defined in

result.ts:152


value

readonly value: T

Implementation of

Resultable.value

Defined in

result.ts:151

Methods

_unsafeUnwrap()

_unsafeUnwrap(config?): T

This method is unsafe, and should only be used in a test environments

Takes a Result<T, E> and returns a T when the result is an Ok, otherwise it throws a custom object.

Parameters

config?: ErrorConfig

Returns

T

Implementation of

Resultable._unsafeUnwrap

Defined in

result.ts:416


_unsafeUnwrapErr()

_unsafeUnwrapErr(config?): E

This method is unsafe, and should only be used in a test environments

takes a Result<T, E> and returns a E when the result is an Err, otherwise it throws a custom object.

Parameters

config?: ErrorConfig

Returns

E

Implementation of

Resultable._unsafeUnwrapErr

Defined in

result.ts:432


[iterator]()

[iterator](): Generator<Result<never, E>, T, any>

Returns

Generator<Result<never, E>, T, any>

Defined in

result.ts:440


andThen()

andThen<X, Y>(f): Result<X, E | Y>

Similar to map Except you must return a new Result.

This is useful for when you need to do a subsequent computation using the inner T value, but that computation might fail. Additionally, andThen is really useful as a tool to flatten a Result<Result<A, E2>, E1> into a Result<A, E2> (see example below).

Type Parameters

X

Y

Parameters

f

The function to apply to the current value

Returns

Result<X, E | Y>

Defined in

result.ts:226


asyncAndThen()

asyncAndThen<X, Y>(f): ResultAsync<X, E | Y>

Similar to map Except you must return a new Result.

This is useful for when you need to do a subsequent async computation using the inner T value, but that computation might fail. Must return a ResultAsync

Type Parameters

X

Y

Parameters

f

The function that returns a ResultAsync to apply to the current value

Returns

ResultAsync<X, E | Y>

Defined in

result.ts:282


asyncMap()

asyncMap<X>(f): ResultAsync<X, E>

Maps a Result<T, E> to ResultAsync<U, E> by applying an async function to a contained Ok value, leaving an Err value untouched.

Type Parameters

X

Parameters

f

An async function to apply an OK value

Returns

ResultAsync<X, E>

Defined in

result.ts:298


finally()

finally<X, Y>(f): DisposableResult<X, Y>

This method is used to clean up and release any resources that the Result

Type Parameters

X = T

Y = E

Parameters

f

The function that will be called to clean up the resources

Returns

DisposableResult<X, Y>

Defined in

result.ts:390


if()

if(fCondition): object

Parameters

fCondition

Returns

object

true()

true: <X1, Y1>(fTrue) => object

Type Parameters

X1

Y1

Parameters

fTrue

Returns

object

false()

false: <X2, Y2>(fFalse) => Result<X1 | X2, E | Y1 | Y2>

Type Parameters

X2

Y2

Parameters

fFalse

Returns

Result<X1 | X2, E | Y1 | Y2>

Defined in

result.ts:234


isErr()

isErr(): boolean

Used to check if a Result is an Err

Returns

boolean

true if the result is an Err variant of Result

Implementation of

Resultable.isErr

Defined in

result.ts:178


isOk()

isOk(): boolean

Used to check if a Result is an OK

Returns

boolean

true if the result is an OK variant of Result

Implementation of

Resultable.isOk

Defined in

result.ts:169


log()

log(fn): this

Performs a side effect for the Ok or Err variant of Result.

Parameters

fn

The function to apply an OK value

Returns

this

the result of applying f or an Err untouched

Defined in

result.ts:346


map()

map<X>(f): Result<X, E>

Maps a Result<T, E> to Result<U, E> by applying a function to a contained Ok value, leaving an Err value untouched.

Type Parameters

X

Parameters

f

The function to apply an OK value

Returns

Result<X, E>

the result of applying f or an Err untouched

Defined in

result.ts:190


mapErr()

mapErr<X>(fn): Result<T, X>

Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value untouched.

This function can be used to pass through a successful result while handling an error.

Type Parameters

X

Parameters

fn

Returns

Result<T, X>

Defined in

result.ts:208


match()

match<A, B>(fnOk, fnErr): A | B

Given 2 functions (one for the Ok variant and one for the Err variant) execute the function that matches the Result variant.

Match callbacks do not necessitate to return a Result, however you can return a Result if you want to.

match is like chaining map and mapErr, with the distinction that with match both functions must have the same return type.

Type Parameters

A

B = A

Parameters

fnOk

fnErr

Returns

A | B

Defined in

result.ts:332


orElse()

orElse<R>(f): Result<T | InferOkTypes<R>, InferErrTypes<R>>

Takes an Err value and maps it to a Result<T, SomeNewType>.

This is useful for error recovery.

Type Parameters

R extends Result<unknown, unknown>

Parameters

f

A function to apply to an Err value, leaving Ok values untouched.

Returns

Result<T | InferOkTypes<R>, InferErrTypes<R>>

Defined in

result.ts:261


safeUnwrap()

safeUnwrap(): Generator<Result<never, E>, T, any>

Returns

Generator<Result<never, E>, T, any>

Defined in

result.ts:451


tap()

tap(fn): this

Performs a side effect for the Ok variant of Result.

Parameters

fn

The function to apply an OK value

Returns

this

the result of applying f or an Err untouched

Defined in

result.ts:360


tapError()

tapError(fn): this

Performs a side effect for the Err variant of Result.

Parameters

fn

The function to apply an Err value

Returns

this

the result of applying f or an Ok untouched

Defined in

result.ts:376


unwrapOr()

unwrapOr<A>(defaultValue): T | A

Unwrap the Ok value, or return the default if there is an Err

Type Parameters

A

Parameters

defaultValue: A

Returns

T | A

Implementation of

Resultable.unwrapOr

Defined in

result.ts:311


unwrapOrThrow()

unwrapOrThrow(): T

Returns

T

Defined in

result.ts:400


combine()

combine(resultList)

static combine<T>(resultList): CombineResults<T>

Type Parameters

T extends readonly [Result<unknown, unknown>, Result<unknown, unknown>]

Parameters

resultList: T

Returns

CombineResults<T>

Defined in

result.ts:127

combine(resultList)

static combine<T>(resultList): CombineResults<T>

Type Parameters

T extends readonly Result<unknown, unknown>[]

Parameters

resultList: T

Returns

CombineResults<T>

Defined in

result.ts:130


combineWithAllErrors()

combineWithAllErrors(resultList)

static combineWithAllErrors<T>(resultList): CombineResultsWithAllErrorsArray<T>

Type Parameters

T extends readonly [Result<unknown, unknown>, Result<unknown, unknown>]

Parameters

resultList: T

Returns

CombineResultsWithAllErrorsArray<T>

Defined in

result.ts:139

combineWithAllErrors(resultList)

static combineWithAllErrors<T>(resultList): CombineResultsWithAllErrorsArray<T>

Type Parameters

T extends readonly Result<unknown, unknown>[]

Parameters

resultList: T

Returns

CombineResultsWithAllErrorsArray<T>

Defined in

result.ts:142


err()

static err<T, E>(error): Result<T, E>

Type Parameters

T = never

E = unknown

Parameters

error: E

Returns

Result<T, E>

Defined in

result.ts:123


fromThrowable()

static fromThrowable<Fn, E>(fn, errorFn?): (...args) => Result<ReturnType<Fn>, E>

Wraps a function with a try catch, creating a new function with the same arguments but returning Ok if successful, Err if the function throws

Type Parameters

Fn extends (...args) => unknown

E

Parameters

fn: Fn

function to wrap with ok on success or err on failure

errorFn?

when an error is thrown, this will wrap the error result if provided

Returns

Function

Parameters

• ...args: Parameters<Fn>

Returns

Result<ReturnType<Fn>, E>

Defined in

result.ts:85


ok()

static ok<T, E>(value): Result<T, E>

Creates a new Result instance representing a successful operation.

Type Parameters

T

E = never

Parameters

value: T

The value to be wrapped in the Result instance.

Returns

Result<T, E>

A new Result instance with the provided value.

Defined in

result.ts:110


unit()

static unit<E>(): Result<undefined, E>

Creates a new Result instance representing a successful operation with an undefined value.

Type Parameters

E = never

Returns

Result<undefined, E>

A new Result instance with an undefined value.

Defined in

result.ts:119

Clone this wiki locally