Skip to content

Result

Result: object

Defined in: Core/Result.ts:26

ap: <E, A>(arg) => <B>(data) => Result<E, B>

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

E

A

Result<E, A>

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

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

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

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

E1

E2

A

B

(e) => E2

(a) => B

(data) => Result<E2, B>

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

bind: <K, E, A, B>(key, f) => (data) => Result<E, A & { [P in string]: B }>

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

K extends string

E

A

B

K

(a) => Result<E, B>

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

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

bindTo: <K>(key) => <E, A>(data) => 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>(data) => Result<E, { [P in string]: A }>

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

chain: <E2, A, B>(f) => <E1>(data) => Result<E2 | E1, B>

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

E2

A

B

(a) => Result<E2, B>

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

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, E2>(predicate, onFail) => <E1>(data) => Result<E2 | E1, A>

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

A

E2

(a) => boolean

(a) => E2

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

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

fold: <E, A, B>(onErr, onOk) => (data) => B

Extracts the value from a Result by providing handlers for both cases.

E

A

B

(e) => B

(a) => B

(data) => B

pipe(
  Result.make.ok(5),
  Result.fold(
    e => `Error: ${e}`,
    n => `Value: ${n}`
  )
); // "Value: 5"

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>

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, 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, A>

pipe(null, Result.from.nullable(() => "is null")); // Err("is null")
pipe(42, Result.from.nullable(() => "is null"));   // Ok(42)

Predicate: <E, A>(pred, onFalse) => (a) => Result<E, A>

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

E

A

(a) => boolean

(a) => E

(a) => Result<E, A>

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, A>(combineErrors) => (val) => 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

A

(errors) => E2

(val) => Result<E2, A>

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

getOrElse: <B>(defaultValue) => <E, A>(data) => 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

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

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>(data) => data is Err<E> = isErr

Type guard that checks if a Result is Err.

E

A

Result<E, A>

data is Err<E>

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

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

Type guard that checks if a Result is Ok.

E

A

Result<E, A>

data is Ok<A>

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

make: object

err: <E>(e) => 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: <E, A, B>(f) => (data) => Result<E, B>

Transforms the success value inside a Result.

E

A

B

(a) => B

(data) => Result<E, B>

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, A>(f) => (data) => Result<F, A>

Transforms the error value inside a Result.

E

F

A

(e) => F

(data) => Result<F, A>

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

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

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

E

A

B

(e) => B

(a) => B

(data) => B

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

recover: <E, B>(fallback) => <A>(data) => Result<E, B | A>

Recovers from an error by providing a fallback Result. The fallback can produce a different success type, widening the result to Result<E, A | B>.

E

B

(e) => Result<E, B>

<A>(data) => Result<E, B | A>

recoverUnless: <E, B>(isBlocked, fallback) => <A>(data) => Result<E, 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<E, A | B>.

E

B

(e) => boolean

() => Result<E, B>

<A>(data) => Result<E, B | A>

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

struct: <E, R>(fields) => Result<E, R>

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

E

R extends Record<string, any>

{ [K in string | number | symbol]: Result<E, R[K]> }

Result<E, R>

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

tap: <E, A>(f) => (data) => Result<E, A>

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

E

A

(a) => void

(data) => Result<E, A>

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

tapError: <E, A>(f) => (data) => Result<E, A>

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

E

A

(e) => void

(data) => Result<E, A>

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

to: object

Maybe: <E, A>(data) => 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.to.Maybe(Result.make.ok(42)); // Some(42)
Result.to.Maybe(Result.make.err("oops")); // None

Validation: <E, A>(data) => 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.to.Validation(Result.make.ok(42));     // Passed(42)
Result.to.Validation(Result.make.err("bad")); // Failed(["bad"])

transposeMaybe: <E, A>(data) => 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.transposeMaybe(Result.make.ok(Maybe.make.some(42))); // Some(Ok(42))
Result.transposeMaybe(Result.make.ok(Maybe.make.none()));   // None
Result.transposeMaybe(Result.make.err("error"));           // Some(Err("error"))

tryCatch: <E, A>(f, 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

(e) => E

Result<E, A>

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