better-result for typed TypeScript errors
Notes on better-result, a TypeScript Result library with tagged errors, generator composition, explicit defect handling, and schema-aware boundaries.
better-result is a TypeScript library for making expected failures part of function return types instead of hiding them behind exceptions.
The core type is familiar: Result<T, E> is either Ok<T> or Err<E>. The library has no runtime dependencies, requires TypeScript 5.4+, and is ESM-only.
What is worth keeping
-
TaggedErrorgives errors useful structure. Errors are realErrorsubclasses but also have literal_tagvalues and typed properties. This makes error unions easy to narrow and match without depending only oninstanceof. -
Result.genmakes composition less noisy. Multiple fallible operations can useyield*and read like normal sequential code. FirstErrstops workflow, while TypeScript keeps inferred union of possible errors. This avoids longandThenchains while keeping failure types visible. -
Expected errors and defects stay separate. Recoverable cases belong in
Err. Unexpected throws inside Result callbacks becomePanic. This prevents programmer bugs and broken invariants from silently becoming another domain error variant. -
Throwing APIs have explicit adapters.
Result.tryandResult.tryPromiseconvert exceptions into Results. Async version also supports retry policies, backoff, jitter, and cancellation. -
Result values can cross boundaries.
Result.codecuses Standard Schema-compatible validators for serialization and deserialization. This looks useful around RPC, persistence, queues, or server actions where in-memory types are not enough.
What I would check before adopting it
The main question is whether generator-based composition stays readable in a larger codebase, especially when mixed with existing code that throws exceptions or already has its own error conventions.
Still, better-result looks worth keeping as a reference when I need typed domain errors without moving to a much larger effect system.
References and copyright
- better-result documentation: better-result project.