Skip to main content

Install NeverThrow

You can install NeverThrow using your preferred package manager:
npm install neverthrow

Import the library

NeverThrow exports the following main types and functions:
import {
  ok,
  Ok,
  err,
  Err,
  Result,
  okAsync,
  errAsync,
  ResultAsync,
  fromAsyncThrowable,
  fromThrowable,
  fromPromise,
  fromSafePromise,
  safeTry,
} from 'neverthrow'

Core types

  • ok - Convenience function to create an Ok variant of Result
  • err - Convenience function to create an Err variant of Result
  • Ok - Ok class and type
  • Err - Err class and type
  • Result - Type alias and namespace containing static methods like Result.fromThrowable and Result.combine

Async types

  • okAsync - Creates a ResultAsync containing an Ok type Result
  • errAsync - Creates a ResultAsync containing an Err type Result
  • ResultAsync - Class for asynchronous result handling

Utility functions

  • fromThrowable - Wraps a throwing function into a Result-returning function
  • fromAsyncThrowable - Wraps an async throwing function into a ResultAsync-returning function
  • fromPromise - Converts a Promise into a ResultAsync
  • fromSafePromise - Converts a non-throwing Promise into a ResultAsync
  • safeTry - Generator-based syntax for ergonomic error propagation
Highly recommended: Install eslint-plugin-neverthrow to ensure errors are not left unhandled.
The ESLint plugin was created as part of NeverThrow’s bounty program by mdbetancourt. It enforces that Result instances are properly consumed, similar to Rust’s must-use attribute.
npm install eslint-plugin-neverthrow

How it works

With eslint-plugin-neverthrow, you must consume Result instances in one of these three ways:
1

Using .match

Handle both Ok and Err cases with pattern matching:
result.match(
  (value) => console.log('Success:', value),
  (error) => console.error('Error:', error)
)
2

Using .unwrapOr

Unwrap the value or provide a default:
const value = result.unwrapOr(defaultValue)
3

Using ._unsafeUnwrap

Unwrap in test environments (not recommended for production):
const value = result._unsafeUnwrap()
This ensures you’re explicitly handling errors instead of silently ignoring them.
The ESLint plugin helps catch unhandled Results at development time, preventing errors from reaching production.

TypeScript configuration

NeverThrow is written in TypeScript and includes type definitions out of the box. For the best experience, ensure you’re using TypeScript 4.1 or later with strictNullChecks enabled in your tsconfig.json:
tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true
  }
}

Next steps

Quick start

Learn how to use Result types in under 5 minutes

Build docs developers (and LLMs) love