← All packages

@lacspace/result

v1.0.0Core Runtime Kit0 deps

Typed, functional error handling — Result<T,E> (Ok/Err) and Option<T> (Some/None) so functions return errors as values instead of throwing. Constructors, guards, map/andThen/match, unwrap variants, trySync/tryAsync to wrap throwing code, and an all() combinator that short-circuits on the first error. Tree-shakeable, zero-dependency, isomorphic.

npm i @lacspace/result

Usage

result.ts
import { trySync, match, all, ok, err } from "@lacspace/result";

// wrap throwing code — errors become values, not exceptions
const parsed = trySync(() => JSON.parse(input)); // Result<any, Error>

const message = match(parsed, {
  ok: (v) => `parsed: ${JSON.stringify(v)}`,
  err: (e) => `bad json: ${e.message}`,
});

// chain fallible steps; the first Err short-circuits
const combined = all([ok(1), ok(2), ok(3)]); // Ok([1, 2, 3])
const stopped = all([ok(1), err("nope"), ok(3)]); // Err("nope")

// async, too
const res = await tryAsync(() => fetch(url).then((r) => r.json()));

Exports 16

okerrsomenonefromNullableisOkisErrmapandThenunwrapunwrapOrmatchtrySynctryAsyncokOrall

Keywords

result-typeoption-typeeitherneverthrow-alternativets-resultsfunctionalerror-handlingrailway-orientedtype-safezero-dependency

More in Core Runtime Kit