Skip to main content
NeverThrow - Type-safe error handling

What is NeverThrow?

NeverThrow helps you encode failure into your program using a Result type that represents either success (Ok) or failure (Err). This eliminates the need for try-catch blocks and makes error handling explicit and type-safe.
import { ok, err, Result } from 'neverthrow'

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) {
    return err('Division by zero')
  }
  return ok(a / b)
}

const result = divide(10, 2)
  .map(value => value * 2)
  .unwrapOr(0)
// result = 10
For asynchronous tasks, NeverThrow offers ResultAsync, which wraps a Promise<Result<T, E>> and gives you the same level of expressivity as a regular Result.

Key features

Type-safe errors

Errors are part of your function signature, making them impossible to ignore and fully type-checked by TypeScript.

Functional patterns

Chain operations with map, andThen, orElse, and other functional methods for elegant error handling.

Async support

ResultAsync is thenable and behaves like a native Promise, but with full Result API access without awaiting.

Safe wrapping

Convert throwing functions to Results with fromThrowable and promises to ResultAsync with fromPromise.

Combine results

Aggregate multiple Results with Result.combine - short-circuits on first error or collects all errors.

Generator syntax

Use safeTry with generator functions for ergonomic error propagation similar to Rust’s ? operator.

Next steps

Installation

Install NeverThrow and optional ESLint plugin

Quick start

Learn the basics in under 5 minutes

Core concepts

Deep dive into Result and ResultAsync types

API reference

Browse the complete API documentation
Looking for real-world examples? Check out this production server implementation: parlez-vous/server

Why not just throw?

Throwing and catching exceptions has several drawbacks:
  • Hidden control flow: Similar to goto statements, making code harder to reason about
  • Unchecked assumptions: You assume the caller will implement catch, which is a known source of bugs
  • Unhandled edge cases: One dev throws, another uses the function without knowing it throws, leading to unhappy users
With Result, errors are explicit, type-checked, and impossible to ignore.
Good to know: Despite the package name, there are valid use cases for throwing. NeverThrow simply encourages you to think more carefully about error handling ergonomics.

Build docs developers (and LLMs) love