What is better-result?
better-result provides aResult<T, E> type that represents either success (Ok) or failure (Err). Instead of throwing exceptions, functions return Results that you can chain, transform, and compose using a clean, functional API.
Why use Result types?
Type-Safe Errors
Errors are part of your function’s type signature, making error handling explicit and impossible to forget.
Generator Composition
Use
yield* syntax to chain multiple Results without nested callbacks or complex control flow.Tagged Errors
Create discriminated error unions with the
TaggedError factory for exhaustive pattern matching.Zero Dependencies
Lightweight and minimal — only 830 lines of core code with no runtime dependencies.
Quick Navigation
Installation
Get started with npm, yarn, pnpm, or bun
Quickstart
Build your first Result-based application
Core Concepts
Learn the fundamentals of Result types
API Reference
Explore the complete API
Key Features
Generator-Based Composition
Chain multiple Results using generator syntax — no nested callbacks or complex error handling logic:Tagged Errors for Type Safety
Create discriminated error unions for exhaustive pattern matching:Async Operations with Retry
Wrap promises and add retry logic with exponential backoff:Serialization for RPC
Convert Results to plain objects for server actions, RPC, or storage:Philosophy
better-result follows these principles:- Errors are values — Not exceptions to be thrown and caught, but first-class values to be transformed and composed
- Type safety first — Error types are part of your function signatures, making error handling explicit
- Functional composition — Chain operations declaratively without complex control flow
- Zero magic — Simple, transparent implementation with no hidden behavior
Next Steps
Install the package
Follow the installation guide to add better-result to your project
Try the quickstart
Build a working example with the quickstart tutorial
Learn the concepts
Dive deeper into Result types and error handling
Explore the API
Browse the complete API reference