Skip to main content

ok

Creates a success Result.
value
T
required
The success value to wrap
return
ResultOk<T>
A success Result containing the value

Example

import { ok } from "@temelj/result";

const result = ok(42);
console.log(result); // { kind: "ok", value: 42 }

err

Creates an error Result.
error
E
required
The error value to wrap
return
ResultErr<E>
An error Result containing the error

Example

import { err } from "@temelj/result";

const result = err("Invalid input");
console.log(result); // { kind: "error", error: "Invalid input" }

isOk

Type guard that checks if a Result is a success.
result
Result<T, E>
required
The Result to check
return
result is ResultOk<T>
True if the Result is a success, false otherwise

Example

import { ok, isOk } from "@temelj/result";

const result = ok(42);
if (isOk(result)) {
  console.log(result.value); // TypeScript knows result.value exists
}

isErr

Type guard that checks if a Result is an error.
result
Result<T, E>
required
The Result to check
return
result is ResultErr<E>
True if the Result is an error, false otherwise

Example

import { err, isErr } from "@temelj/result";

const result = err("Failed");
if (isErr(result)) {
  console.log(result.error); // TypeScript knows result.error exists
}

unwrap

Extracts the success value from a Result or throws the error.
result
Result<T, E>
required
The Result to unwrap
return
T
The success value if the Result is Ok
throws
E
Throws the error value if the Result is Err

Example

import { ok, err, unwrap } from "@temelj/result";

const success = ok(42);
console.log(unwrap(success)); // 42

const failure = err("Invalid");
unwrap(failure); // Throws "Invalid"

unwrapErr

Extracts the error value from a Result or throws an error.
result
Result<T, E>
required
The Result to unwrap
return
E
The error value if the Result is Err
throws
Error
Throws an Error if the Result is Ok

Example

import { err, unwrapErr } from "@temelj/result";

const failure = err("Invalid input");
console.log(unwrapErr(failure)); // "Invalid input"

unwrapOr

Extracts the success value from a Result or returns a default value.
result
Result<T, E>
required
The Result to unwrap
defaultValue
T | (() => T)
required
The default value to return if the Result is an error. Can be a value or a function that returns a value.
return
T
The success value if Ok, otherwise the default value

Example

import { ok, err, unwrapOr } from "@temelj/result";

const success = ok(42);
console.log(unwrapOr(success, 0)); // 42

const failure = err("Invalid");
console.log(unwrapOr(failure, 0)); // 0

// With a function
console.log(unwrapOr(failure, () => Math.random())); // Random number

map

Maps the success value of a Result to a new value.
result
Result<T, E>
required
The Result to map
fn
(value: T) => U
required
Function to transform the success value
return
Result<U, E>
A new Result with the transformed value, or the original error

Example

import { ok, err, map } from "@temelj/result";

const result = ok(5);
const doubled = map(result, (x) => x * 2);
console.log(doubled); // { kind: "ok", value: 10 }

const error = err("Invalid");
const mapped = map(error, (x) => x * 2);
console.log(mapped); // { kind: "error", error: "Invalid" }

mapErr

Maps the error value of a Result to a new error value.
result
Result<any, E>
required
The Result to map
fn
(error: E) => F
required
Function to transform the error value
return
Result<any, F>
A new Result with the transformed error, or the original success value

Example

import { err, mapErr } from "@temelj/result";

const result = err("failed");
const mapped = mapErr(result, (e) => e.toUpperCase());
console.log(mapped); // { kind: "error", error: "FAILED" }

fromThrowable

Calls a function that may throw and returns a Result.
fn
() => T
required
The synchronous function to execute
onErr
(e: unknown) => E
Optional function to convert the unknown thrown value to type E
return
Result<T, E | unknown>
A Result containing either the function’s return value or the caught error

Example

import { fromThrowable } from "@temelj/result";

const result = fromThrowable(() => {
  return JSON.parse("{invalid json}");
});

const withErrorMapping = fromThrowable(
  () => JSON.parse("{invalid}"),
  (e) => `Parse error: ${e}`
);

fromPromise

Calls a function that returns a Promise and returns a Promise<Result>.
fn
() => Promise<T>
required
The async function to execute
onErr
(e: unknown) => E
Optional function to convert the unknown rejection reason to type E
return
Promise<Result<T, E | unknown>>
A Promise of a Result containing either the resolved value or the caught error

Example

import { fromPromise } from "@temelj/result";

const result = await fromPromise(async () => {
  const response = await fetch("/api/data");
  return response.json();
});

const withErrorMapping = await fromPromise(
  async () => fetch("/api/data").then((r) => r.json()),
  (e) => `Network error: ${e}`
);

Build docs developers (and LLMs) love