Typed Result Type Instead Of Throwing
A Result union with map, mapErr and andThen combinators that makes error paths explicit and compiler-checked instead of exception driven.
Code
export type Result<T, E> =
| { readonly ok: true; readonly value: T }
| { readonly ok: false; readonly error: E };
export function ok<T>(value: T): Result<T, never> {
return { ok: true, value };
}
export function err<E>(error: E): Result<never, E> {
return { ok: false, error };
}
export function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> {
return result.ok ? ok(fn(result.value)) : result;
}
export function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {
return result.ok ? result : err(fn(result.error));
}
export function andThen<T, U, E>(
result: Result<T, E>,
fn: (value: T) => Result<U, E>,
): Result<U, E> {
return result.ok ? fn(result.value) : result;
}
export function unwrapOr<T, E>(result: Result<T, E>, fallback: T): T {
return result.ok ? result.value : fallback;
}Exceptions are invisible in the type system, so callers forget to handle failure paths and any refactor can silently drop handling.
Use case: TypeScript engineers who want errors in the type signature
Inputs / outputs
- Input: success values and error values wrapped in Ok/Err
- Output: Result values composed through map, mapErr, andThen without try/catch
Error conditions
- mapping a function over an Err keeps the error
- andThen chains only on Ok
- unwrapOr supplies a fallback for Err
Approach
discriminated union with ok flag; combinators branch on the flag and preserve types; no throw in the combinators themselves.
Implementation
export type Result<T, E> =
| { readonly ok: true; readonly value: T }
| { readonly ok: false; readonly error: E };
export function ok<T>(value: T): Result<T, never> {
return { ok: true, value };
}
export function err<E>(error: E): Result<never, E> {
return { ok: false, error };
}
export function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> {
return result.ok ? ok(fn(result.value)) : result;
}
export function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {
return result.ok ? result : err(fn(result.error));
}
export function andThen<T, U, E>(
result: Result<T, E>,
fn: (value: T) => Result<U, E>,
): Result<U, E> {
return result.ok ? fn(result.value) : result;
}
export function unwrapOr<T, E>(result: Result<T, E>, fallback: T): T {
return result.ok ? result.value : fallback;
}
Verification tests
import { test } from "node:test";
import assert from "node:assert/strict";
import { andThen, err, map, mapErr, ok, unwrapOr, type Result } from "./example.ts";
test("map transforms Ok and skips Err", () => {
assert.deepEqual(map(ok(2), (n) => n * 3), ok(6));
const failure: Result<number, string> = err("boom");
assert.deepEqual(map(failure, (n) => n * 3), err("boom"));
});
test("mapErr transforms only the error path", () => {
assert.deepEqual(mapErr(err("boom"), (e) => e.length), err(4));
const untouched: Result<number, never> = ok(1);
assert.deepEqual(mapErr(untouched, (e: never) => String(e)), ok(1));
});
test("andThen chains and short-circuits", () => {
const parse = (s: string): Result<number, string> => {
const n = Number(s);
return Number.isFinite(n) ? ok(n) : err("not a number");
};
const positive = (n: number): Result<number, string> =>
n > 0 ? ok(n) : err("not positive");
assert.deepEqual(andThen(parse("5"), positive), ok(5));
assert.deepEqual(andThen(parse("-5"), positive), err("not positive"));
assert.deepEqual(andThen(parse("x"), positive), err("not a number"));
});
test("unwrapOr returns fallback for Err", () => {
assert.equal(unwrapOr(ok(7), 0), 7);
assert.equal(unwrapOr(err("x"), 0), 0);
});
Dependencies and running
No external dependencies (standard library only).
Runtime: node >= 18 Entry point: node –experimental-strip-types example.ts
Limitations and error handling
no async combinators; no collection helpers (all/any) in this scope.
Verification boundary
execution tests ran offline in an isolated temp dir with a sanitized environment; no network, no writes outside the sandbox
Interactive Tools for This Topic
Report a problem with this exampleOpens a pre-filled issue on GitHub.