Install NeverThrow
You can install NeverThrow using your preferred package manager:Import the library
NeverThrow exports the following main types and functions:Core types
ok- Convenience function to create anOkvariant ofResulterr- Convenience function to create anErrvariant ofResultOk- Ok class and typeErr- Err class and typeResult- Type alias and namespace containing static methods likeResult.fromThrowableandResult.combine
Async types
okAsync- Creates aResultAsynccontaining anOktypeResulterrAsync- Creates aResultAsynccontaining anErrtypeResultResultAsync- Class for asynchronous result handling
Utility functions
fromThrowable- Wraps a throwing function into a Result-returning functionfromAsyncThrowable- Wraps an async throwing function into a ResultAsync-returning functionfromPromise- Converts a Promise into a ResultAsyncfromSafePromise- Converts a non-throwing Promise into a ResultAsyncsafeTry- Generator-based syntax for ergonomic error propagation
ESLint plugin (recommended)
The ESLint plugin was created as part of NeverThrow’s bounty program by mdbetancourt. It enforces thatResult instances are properly consumed, similar to Rust’s must-use attribute.
How it works
Witheslint-plugin-neverthrow, you must consume Result instances in one of these three ways:
This ensures you’re explicitly handling errors instead of silently ignoring them.
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 withstrictNullChecks enabled in your tsconfig.json:
tsconfig.json
Next steps
Quick start
Learn how to use Result types in under 5 minutes