monads
Result/Option types and Rust-style functional combinators for explicit, type-safe error handling.
monads provides Result, ResultAsync, and Option types with Rust-style functional combinators — an explicit, type-safe alternative to throw/catch and nullable checks.
Motivation
Hand-rolled { ok: boolean, ... } result shapes and nullable checks are easy to write once and painful to keep consistent across a codebase. Libraries like neverthrow and Effect solve this well, but pull in more surface (do-notation, a runtime, generators, Either, class-based fluent APIs) than most projects need.
monads is deliberately small: Result, ResultAsync, and Option, as standalone, tree-shakeable functions composed with pipe — not class instances with chained methods.
Features
Result<T, E>—ok/errconstructors,isOk/isErrguards, andResult.map/Result.mapErr/Result.andThen/Result.unwrapOr/Result.unwrapOrElse/Result.unwrap/Result.matchcombinators.ResultAsync<T, E>— a thenable wrapper aroundPromise<Result<T, E>>, awaitable directly, with chainable combinators for building async pipelines before the finalawait.Option<T>—some/noneconstructors,isSome/isNoneguards, and the mirroredOptioncombinators.- Bridges —
fromThrowable,fromPromise,fromNullable. pipe— left-to-right composition for the standalone combinators above.- Zero dependencies, tree-shakeable.
Quick Start
import { err, ok, pipe, Result } from "@zap-studio/monads";
function parseAge(input: string): Result<number, string> {
const value = Number(input);
return Number.isNaN(value) ? err("not a number") : ok(value);
}
const message = pipe(
parseAge("42"),
Result.map((age) => age + 1),
Result.match({
ok: (age) => `Age next year: ${age}`,
err: (reason) => `Invalid input: ${reason}`,
}),
);
Learn More
- Getting Started — install and use your first
Result - Result —
Result<T, E>constructors, guards, and combinators - ResultAsync — chaining async operations that can fail
- Option —
Option<T>constructors, guards, and combinators - Pipe — composing the standalone combinators
Runtime Support
| Runtime | Minimum version |
|---|---|
| Node.js | 18.0.0 |
| Bun | 1.0.0 |
| Deno | 1.42 |
| Cloudflare Workers | Any current release |
| Browsers | Latest evergreen (Chrome, Edge, Firefox, Safari) |
The package ships standard ESM only and uses no runtime-specific APIs. Deno 1.42 is the first release that can install packages from JSR (deno add jsr:@zap-studio/monads).