Result
Result<T, E> = Ok<T, E> | Err<T, E> represents a synchronous success or failure.
ok / err
ok<T, E = never>(value: T): Ok<T, E> / err<T = never, E = unknown>(error: E): Err<T, E>
Use ok for a value and err for the failure value.
isOk / isErr
isOk(): this is Ok<T, E> / isErr(): this is Err<T, E>
Use these type guards when control flow needs the concrete variant.
Result.combine
Result.combine(resultList: readonly Result<unknown, unknown>[]): Result<unknown[], unknown>
Result.combine returns all Ok values or short-circuits with the first Err.
Result.combineWithAllErrors
Result.combineWithAllErrors(resultList: readonly Result<unknown, unknown>[]): Result<unknown[], unknown[]>
Result.combineWithAllErrors collects every Err instead of stopping at the first one.
map
map<A>(f: (value: T) => A): Result<A, E>
map transforms an Ok value and leaves an Err untouched.
mapErr
mapErr<U>(f: (error: E) => U): Result<T, U>
mapErr transforms an Err value and leaves an Ok untouched.
andThen
andThen<U, F>(f: (value: T) => Result<U, F>): Result<U, E | F>
Use andThen when the next synchronous computation can also fail.
andCheck
andCheck<F>(f: (value: T) => Result<unknown, F>): Result<T, E | F>
andCheck keeps the original Ok value when the check succeeds and propagates the check's Err when it fails.
andInspect
andInspect(f: (value: T) => unknown): Result<T, E>
andInspect runs a side effect only on Ok, ignores anything it returns or throws, and keeps the original result.
orInspect
orInspect(f: (error: E) => unknown): Result<T, E>
orInspect runs a side effect only on Err, ignores anything it returns or throws, and keeps the original result.
orElse
orElse<U, F>(f: (error: E) => Result<U, F>): Result<T | U, F>
Use orElse to recover from an Err with another Result.
unwrapOr
unwrapOr<A>(fallback: A): T | A
unwrapOr returns the Ok value or the fallback for an Err.
match
match<A, B = A>(ok: (value: T) => A, err: (error: E) => B): A | B
match handles both variants and returns the selected callback's value.
asyncAndThen
asyncAndThen<U, F>(f: (value: T) => ResultAsync<U, F>): ResultAsync<U, E | F>
Use asyncAndThen to continue a synchronous Result with a fallible asynchronous computation.
asyncAndCheck
asyncAndCheck<F>(f: (value: T) => ResultAsync<unknown, F>): ResultAsync<T, E | F>
asyncAndCheck runs an asynchronous check while preserving the original Ok value when it succeeds.
asyncMap
asyncMap<U>(f: (value: T) => Promise<U>): ResultAsync<U, E>
asyncMap transforms an Ok with a promise and returns a ResultAsync.
fromThrowable
fromThrowable<Fn extends (...args: readonly any[]) => any, E>(fn: Fn, errorFn?: (error: unknown) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>
fromThrowable catches synchronous throws and maps them to Err; it does not catch rejected promises.
safeTry
safeTry<T, E>(body: () => Generator<Err<never, E>, Result<T, E>>): Result<T, E>
safeTry<T, E>(body: () => AsyncGenerator<Err<never, E>, Result<T, E>>): ResultAsync<T, E>
Inside safeTry, yield* unwraps each Ok and returns the first Err; an async generator returns ResultAsync.