@gordonb/result
Typescript functions for working with Rust-like Result and Option types.
The package is written in a pragmatic functional style: just plain data and
functions. Result is { ok: true, value: T } | { ok: false, error: E }. Option
is T | undefined. The rest of the library is just simple utility and
combinator functions. No wrapper classes, no methods.
Install
Deno:
deno add jsr:@gordonb/result
Agent skill
Install the result skill to teach your coding agent this library.
Claude Code:
/plugin marketplace add gordonbrander/result
/plugin install result@result
Codex:
$skill-installer install https://github.com/gordonbrander/result/tree/main/.agents/skills/result
Quick example
import { Option, Result } from "@gordonb/result";
// Result
const parsed = Result.perform(() => JSON.parse(input));
const name = Result.map(parsed, (data) => data.name);
const greeting = Result.unwrapOr(name, "stranger");
// Option
const first: string | undefined = items[0];
const upper = Option.map(first, (s) => s.toUpperCase());
const display = Option.unwrapOr(upper, "(empty)");
Result
import * as Result from "@gordonb/result/result";
Types
type Ok<T> = { ok: true; value: T };
type Err<E> = { ok: false; error: E };
type Result<T, E> = Ok<T> | Err<E>;
Factory functions
ok<T>(value: T): Result<T, never>
err<E>(error: E): Result<never, E>
Result.ok(42); // { ok: true, value: 42 }
Result.err("!"); // { ok: false, error: "!" }
Type guards
isOk<T, E>(result: Result<T, E>): result is Ok<T>
isErr<T, E>(result: Result<T, E>): result is Err<E>
Both are type-narrowing guards that work with if statements and
Array.filter.
Unwrapping
unwrap<T, E>(result: Result<T, E>): T
Returns the Ok value, or throws a TypeError with the error as cause.
unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T
Returns the Ok value, or falls back to defaultValue.
unwrapOrElse<T, E>(result: Result<T, E>, defaultValue: (error: E) => T): T
Returns the Ok value, or computes a fallback from the error.
Transformations
map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>
Transforms the Ok value. Err passes through unchanged.
mapOr<T, U, E>(result: Result<T, E>, fn: (value: T) => U, defaultValue: U): U
Transforms the Ok value, or returns defaultValue if Err.
mapOrElse<T, U, E>(
result: Result<T, E>,
fn: (value: T) => U,
defaultValue: (error: E) => U,
): U
Transforms the Ok value, or computes a fallback from the error.
flatMap<T, U, E>(
result: Result<T, E>,
transform: (value: T) => Result<U, E>,
): Result<U, E>
Transforms the Ok value with a function that itself returns a Result, then
flattens. Useful for chaining operations that can fail.
mapErr<T, E, U>(result: Result<T, E>, fn: (error: E) => U): Result<T, U>
Transforms the Err value. Ok passes through unchanged.
Conversion
toOption<T, E>(result: Result<T, E>): Option<T>
Converts a Result to an Option. Ok is unwrapped, Err becomes undefined.
Error capture
perform<T, E = unknown>(fn: () => T): Result<T, E>
Runs a throwing function and captures the outcome as a Result.
Result.perform(() => JSON.parse(input));
// Ok with parsed value, or Err with the SyntaxError
performAsync<T, E = unknown>(fn: () => Promise<T>): Promise<Result<T, E>>
Async version of perform.
await Result.performAsync(() => fetch(url).then((r) => r.json()));
safeTry and safeTryAsync are aliases for perform and performAsync.
Bridging
intoResult<T, E>(value: Nullish<T>, error: Nullish<E>): Result<Option<T>, E>
Converts two disjoint nullish values (a possible value and a possible error)
into a proper Result. Useful for bridging libraries that return separate
value/error pairs instead of a discriminated union.
const { data, error } = await supabase.from("users").select();
const result = Result.intoResult(data, error);
Bridging with Zod
import { toResult } from "@gordonb/result/result/zod";
Converts a Zod safeParse result into
a Result<T, ZodError>.
import { z } from "zod";
const User = z.object({ name: z.string(), age: z.number() });
const result = toResult(User.safeParse(input));
// Ok with parsed User, or Err with ZodError
The zod module re-declares Zod's SafeParseReturnType locally, so it has no
runtime dependency on Zod. Any object matching the { success, data, error }
shape will work.
Option
Option is modeled as T | undefined, with helpers for coalescing nullish values
and transforming optional data.
import * as Option from "@gordonb/result/option";
Types
type Option<T> = T | undefined;
type Nullish<T> = T | null | undefined;