neverthrow
Result type for TypeScript that models errors in return values instead of exceptions.
Official Documentation ↗v8. Check the official docs for the latest API surface.Installation
npm install neverthrowpnpm add neverthrowMethods
No methods match this filter.
Result type for TypeScript that models errors in return values instead of exceptions.
Overview
neverthrow brings the Result pattern to TypeScript: ok and err construct typed outcomes, map and andThen compose them, and unwrapOr or expect terminate chains. The error type travels with the value, making failure handling visible in every signature.
Result.combine aggregates parallel checks, and safeTry supports async flows with the same guarantees. It is a small dependency with no runtime machinery beyond the variants themselves, which makes it easy to introduce alongside existing exception code.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
ok / err— Constructs success and failure results; the type marks which variant you hold.map / mapErr / andThen— Chains transformations on the success path and switches into new results with andThen.unwrapOr / expect— Terminates a chain with a fallback value or an explicit, documented panic.Result.combine— Aggregates parallel results into one, failing fast with the first error.
Usage Notes
- Use andThen when the next step can fail, map when it cannot.
- Reserve expect for cases that are impossible by construction, and say why.
- Combine many validations once, then handle the single aggregated error.
Common Pitfalls
- Calling _unsafeUnwrap everywhere reintroduces exceptions under a different name.
- Forgetting to return the result of andThen breaks the chain silently.
- Wrapping truly exceptional bugs in err hides programming errors from crash reporting.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/supermacro/neverthrow@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:08.285Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- This package contains a Result type that represents either success (Ok) or failure (Err). (https://cdn.jsdelivr.net/gh/supermacro/neverthrow@latest/README.md)
- As of v4.1.0-beta, you are able to return distinct error types (see signature below). (https://cdn.jsdelivr.net/gh/supermacro/neverthrow@latest/README.md)
- This is useful for when you need to do a subsequent computation using the inner T value, but that computation might fail. (https://cdn.jsdelivr.net/gh/supermacro/neverthrow@latest/README.md)
FAQ
- Why return errors instead of throwing?
- Typed returns appear in signatures, so callers cannot forget the failure path, and control flow stays explicit. Exceptions are invisible in types and jump unpredictably, which is exactly what long pipelines do not need.
- How does asyncResult work?
- safeTry with generator support lets async functions yield* Results and ResultPromises, writing sequential async code that still returns a Result. The runtime behavior mirrors async/await but the error channel stays typed.
- When should I still throw?
- Programmer errors, violated invariants and unreachable states deserve exceptions; expected domain outcomes, validation, not-found, rate limits, should be Results. Mixing deliberately keeps both channels honest.
Report a problem with this API docOpens a pre-filled issue on GitHub.