Skip to content

Result

Result: object

Defined in: Core/Result.ts:27

apply: <E2, A>(arg) => <B, E1>(result) => Result<E2 | E1, B>

Applies a function wrapped in a Result to a value wrapped in a Result.

E2 = never

A = unknown

Result<E2, A>

<B, E1>(result) => Result<E2 | E1, B>

Result.map to apply a plain unary function to an Ok value.

const add = (a: number) => (b: number) => a + b;
pipe(
  Result.make.ok(add),
  Result.apply(Result.make.ok(5)),
  Result.apply(Result.make.ok(3))
); // Ok(8)

assign: <K, E2, A, B>(key, transform) => <E1>(result) => Result<E2 | E1, A & { [P in string]: B }>

Evaluates a new Result using the current accumulator and attaches the output to a new key.

K extends string

E2

A

B

K

(value) => Result<E2, B>

<E1>(result) => Result<E2 | E1, A & { [P in string]: B }>

Result.assignTo to initialize a pipeline accumulator record.

pipe(
  Result.make.ok({ a: 1 }),
  Result.assign("b", ({ a }) => Result.make.ok(a + 1))
); // Ok({ a: 1, b: 2 })

assignTo: <K>(key) => <E, A>(result) => Result<E, { [P in string]: A }>

Converts a Result value into an object containing a single property. Initiates the pipeline accumulator record.

K extends string

K

<E, A>(result) => Result<E, { [P in string]: A }>

Result.assign to append additional fields to an accumulator record.

pipe(Result.make.ok(42), Result.assignTo("value")); // Ok({ value: 42 })

bimap: <E1, E2, A, B>(onErr, onOk) => (result) => Result<E2, B>

Transforms both branches of a Result simultaneously. Applies onErr to Err values and onOk to Ok values.

E1

E2

A

B

(error) => E2

(value) => B

(result) => Result<E2, B>

pipe(
  Result.make.ok(5),
  Result.bimap(
    (e) => `Error: ${e}`,
    (n) => n * 2
  )
); // Ok(10)

chain: <E2, A, B>(transform) => <E1>(result) => Result<E2 | E1, B>

Chains Result computations. If the first is Ok, passes the value to transform. If the first is Err, propagates the error.

E2

A

B

(value) => Result<E2, B>

<E1>(result) => Result<E2 | E1, B>

Result.map to transform the inner value without returning a new Result.

const validatePositive = (n: number): Result<string, number> =>
  n > 0 ? Result.make.ok(n) : Result.make.err("Must be positive");

pipe(Result.make.ok(5), Result.chain(validatePositive)); // Ok(5)
pipe(Result.make.ok(-1), Result.chain(validatePositive)); // Err("Must be positive")

ensure: {<A, B, E2>(refinement, onFail): <E1>(result) => Result<E2 | E1, B>; <A, E2>(predicate, onFail): <E1>(result) => Result<E2 | E1, A>; }

Narrows an Ok value with a predicate, converting to Err(onFail(value)) if the predicate returns false.

<A, B, E2>(refinement, onFail): <E1>(result) => Result<E2 | E1, B>

A

B

E2

(value) => value is B

(value) => E2

<E1>(result) => Result<E2 | E1, B>

<A, E2>(predicate, onFail): <E1>(result) => Result<E2 | E1, A>

A

E2

(value) => boolean

(value) => E2

<E1>(result) => Result<E2 | E1, A>

Result.from.Predicate to lift a value into a Result based on a predicate.

pipe(
  Result.make.ok(15),
  Result.ensure((n) => n >= 18, (n) => `Age ${n} is below 18`)
); // Err("Age 15 is below 18")

from: object

Maybe: <E>(onNone) => <A>(maybe) => Result<E, A>

Creates a Result from a Maybe. Some becomes Ok, None becomes error from onNone.

E

() => E

<A>(maybe) => Result<E, A>

Result.to.Maybe to convert a Result back into a Maybe.

pipe(Maybe.make.none(), Result.from.Maybe(() => "is none")); // Err("is none")
pipe(Maybe.make.some(42), Result.from.Maybe(() => "is none")); // Ok(42)

nullable: <E>(onNull) => <A>(value) => Result<E, NonNullable<A>>

Creates a Result from a nullable value. Returns Ok if the value is not null or undefined, error from onNull otherwise.

E

() => E

<A>(value) => Result<E, NonNullable<A>>

  • Result.from.Maybe to convert an optional Maybe into a Result.
  • Result.from.Predicate to validate and lift values into a Result based on a predicate.
pipe(null, Result.from.nullable(() => "is null")); // Err("is null")
pipe(42, Result.from.nullable(() => "is null"));   // Ok(42)

Predicate: {<A, B, E>(refinement, onFalse): (value) => Result<E, B>; <A, E>(predicate, onFalse): (value) => Result<E, A>; }

Creates a Result from a predicate or type guard applied to a value. Returns Ok if the predicate passes, Err from onFalse otherwise.

<A, B, E>(refinement, onFalse): (value) => Result<E, B>

A

B

E

(value) => value is B

(value) => E

(value) => Result<E, B>

<A, E>(predicate, onFalse): (value) => Result<E, A>

A

E

(value) => boolean

(value) => E

(value) => Result<E, A>

Result.ensure to validate and filter an existing Result.

pipe(5, Result.from.Predicate(n => n > 0, n => `${n} is not positive`));  // Ok(5)
pipe(-1, Result.from.Predicate(n => n > 0, n => `${n} is not positive`)); // Err("-1 is not positive")
pipe("", Result.from.Predicate(s => s.length > 0, () => "empty string")); // Err("empty string")

Validation: <E1, E2>(combineErrors) => <A>(validation) => Result<E2, A>

Converts a Validation to a Result, combining accumulated errors using combineErrors. Passed(a) becomes Ok(a); Failed(errors) becomes Err(combineErrors(errors)).

E1

E2

(errors) => E2

<A>(validation) => Result<E2, A>

Result.to.Validation to convert a Result into a Validation.

Result.from.Validation((errors) => errors.join(", "))(Validation.make.failed("error1")); // Err("error1")

getOrElse: <B>(fallback) => <A, E>(result) => B | A

Returns the success value or a default value if the Result is an error. The default is a thunk () => B — evaluated only when the Result is Err. The default can be a different type, widening the result to A | B.

B

() => B

<A, E>(result) => B | A

Result.match to handle both the Ok and Err cases.

pipe(Result.make.ok(5), Result.getOrElse(() => 0)); // 5
pipe(Result.make.err("error"), Result.getOrElse(() => 0)); // 0
pipe(Result.make.err("error"), Result.getOrElse(() => null)); // null — typed as number | null

is: object

err: <E, A>(result) => result is Err<E> = isErr

Type guard that checks if a Result is Err.

E

A

Result<E, A>

result is Err<E>

Result.is.ok to check if a Result is an Ok success.

const res = Result.make.err("failed");
if (Result.is.err(res)) {
  console.log(res.error); // "failed"
}

ok: <E, A>(result) => result is Ok<A> = isOk

Type guard that checks if a Result is Ok.

E

A

Result<E, A>

result is Ok<A>

Result.is.err to check if a Result is an Err failure.

const res = Result.make.ok(42);
if (Result.is.ok(res)) {
  console.log(res.value); // 42
}

make: object

err: <E>(error) => Err<E> = makeErr

Creates a failed Result with the given error.

E

E

Err<E>

Result.make.err("Error message"); // Err("Error message")

ok: <A>(value) => Ok<A> = makeOk

Creates a successful Result with the given value.

A

A

Ok<A>

Result.make.ok(42); // Ok(42)

map: <A, B>(transform) => <E>(result) => Result<E, B>

Transforms the success value inside a Result.

A

B

(value) => B

<E>(result) => Result<E, B>

  • Result.chain to sequence operations that themselves return a Result.
  • Result.mapError to transform the error value instead of the success value.
pipe(Result.make.ok(5), Result.map(n => n * 2)); // Ok(10)
pipe(Result.make.err("error"), Result.map(n => n * 2)); // Err("error")

mapError: <E, F>(transform) => <A>(result) => Result<F, A>

Transforms the error value inside a Result.

E

F

(error) => F

<A>(result) => Result<F, A>

Result.map to transform the success value instead of the error value.

pipe(Result.make.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")

match: <E, A, B>(cases) => (result) => B

Pattern matches on a Result, returning the result of the matching case.

E

A

B

(error) => B

(value) => B

(result) => B

Result.getOrElse to extract the success value with a default fallback.

pipe(
  result,
  Result.match({
    ok: value => `Got ${value}`,
    err: error => `Failed: ${error}`
  })
);

recover: <E1, B, E2>(fallback) => <A>(result) => Result<E2, B | A>

Recovers from an error by providing a fallback Result. The fallback can produce a different success type or resolve with a different error type.

E1

B

E2 = never

(error) => Result<E2, B>

<A>(result) => Result<E2, B | A>

Result.recoverUnless to conditionally recover based on the error value.

pipe(
  Result.make.err("not found"),
  Result.recover((e) => Result.make.ok(`default for ${e}`))
); // Ok("default for not found")

recoverUnless: <E1, B, E2>(isBlocked, fallback) => <A>(result) => Result<E1 | E2, B | A>

Recovers from an error unless the predicate isBlocked returns true for that error. The fallback can produce a different success type, widening the result to Result<E1 | E2, A | B>.

E1

B

E2 = never

(error) => boolean

(error) => Result<E2, B>

<A>(result) => Result<E1 | E2, B | A>

Result.recover for unconditional error recovery.

pipe(
  Result.make.err(new Error("not found")),
  Result.recoverUnless(e => e.message === "fatal", () => Result.make.ok(0))
); // Ok(0)

sequence: object

Maybe: <E, A>(result) => Maybe<Result<E, A>>

Swaps the outer Result and inner Maybe context. Ok(Some(a)) becomes Some(Ok(a)), Ok(None) becomes None, and Err(e) becomes Some(Err(e)).

E

A

Result<E, Maybe<A>>

Maybe<Result<E, A>>

Result.sequence.Maybe(Result.make.ok(Maybe.make.some(42))); // Some(Ok(42))
Result.sequence.Maybe(Result.make.ok(Maybe.make.none()));   // None
Result.sequence.Maybe(Result.make.err("error"));           // Some(Err("error"))

struct: <T>(fields) => Result<T[keyof T] extends Result<E, unknown> ? E : never, { [K in string | number | symbol]: T[K] extends Result<unknown, A> ? A : never }>

Combines a record of Results into a single Result of a record. Evaluates fields in key order and short-circuits on the first failure.

T extends Record<string, Result<unknown, unknown>>

T

Result<T[keyof T] extends Result<E, unknown> ? E : never, { [K in string | number | symbol]: T[K] extends Result<unknown, A> ? A : never }>

Result.assign to combine values sequentially with inter-field dependencies.

Result.struct({
  name: Result.make.ok("Alice"),
  age: Result.make.ok(30)
}); // Ok({ name: "Alice", age: 30 })

tap: <A>(sideEffect) => <E>(result) => Result<E, A>

Executes a side effect on the success value without changing the Result. Useful for logging or debugging.

A

(value) => void

<E>(result) => Result<E, A>

Result.tapError to perform a side effect on the error value.

pipe(
  Result.make.ok(5),
  Result.tap(n => console.log("Value:", n)),
  Result.map(n => n * 2)
);

tapError: <E>(sideEffect) => <A>(result) => Result<E, A>

Executes a side effect on the error value without changing the Result. Useful for logging or reporting errors.

E

(error) => void

<A>(result) => Result<E, A>

Result.tap to perform a side effect on the success value.

pipe(
  Result.make.err("not found"),
  Result.tapError(e => console.error("validation failed:", e)),
  Result.chain(save),
)

to: object

Maybe: <E, A>(result) => Maybe<A>

Converts a Result to a Maybe. Ok becomes Some, Err becomes None (the error is discarded).

E

A

Result<E, A>

Maybe<A>

Result.from.Maybe to convert a Maybe into a Result.

Result.to.Maybe(Result.make.ok(42)); // Some(42)
Result.to.Maybe(Result.make.err("oops")); // None

Validation: <E, A>(result) => Validation<E, A>

Converts a Result to a Validation. Ok(a) becomes Passed(a); Err(e) becomes Failed([e]).

E

A

Result<E, A>

Validation<E, A>

Result.from.Validation to convert a Validation into a Result.

Result.to.Validation(Result.make.ok(42));     // Passed(42)
Result.to.Validation(Result.make.err("bad")); // Failed(["bad"])

tryCatch: <E, A>(operation, options) => Result<E, A>

Creates a Result from a synchronous thunk that may throw. Catches any errors and transforms them using the onError function.

E

A

() => A

(error) => E

Result<E, A>

const result = Result.tryCatch(
  () => JSON.parse(rawString),
  { onError: (error) => `Parse error: ${error}` }
);