Frequently Asked Questions
Find answers to common questions about NeverThrow, Result types, and error handling patterns.General Questions
Why is the package called 'neverthrow'?
Why is the package called 'neverthrow'?
neverthrow, don’t take this literally. The name encourages developers to think more carefully about the ergonomics and usage of their software.Throwing and catching exceptions is very similar to using goto statements - it makes reasoning about programs harder. Additionally, by using throw you make the assumption that the caller of your function is implementing catch, which is a known source of errors.Example scenario: One developer throws an error, and another developer uses the function without prior knowledge that it will throw. An edge case has been left unhandled, leading to unhappy users.That said, there are definitely legitimate use cases for throwing in your program - just far fewer than you might think.What is a Result type?
What is a Result type?
Result type represents either success (Ok) or failure (Err). This makes it impossible to ignore errors because the error is explicitly part of the type signature.Instead of:How does NeverThrow compare to try/catch?
How does NeverThrow compare to try/catch?
- Errors are not visible in type signatures
- Easy to forget to handle errors
- Stack traces can be lost
- Control flow is implicit
- Errors are explicit in type signatures
- Compiler forces error handling
- Composable error handling with
map,andThen, etc. - Control flow is explicit and easier to reason about
What runtimes does NeverThrow support?
What runtimes does NeverThrow support?
- Node.js (v18+)
- Browsers (all modern browsers)
- Deno
- Bun
- Any JavaScript runtime that supports ES6
engines field in package.json specifies Node.js v18+ and npm v11+ primarily for maintaining a consistent development environment, not as a hard runtime requirement.Can I use NeverThrow with JavaScript (not TypeScript)?
Can I use NeverThrow with JavaScript (not TypeScript)?
Working with Results
When should I use Result vs ResultAsync?
When should I use Result vs ResultAsync?
Result for synchronous operations:ResultAsync for asynchronous operations:ResultAsync is thenable and behaves like a native Promise, but with additional methods.How do I convert a throwing function to use Results?
How do I convert a throwing function to use Results?
Result.fromThrowable or ResultAsync.fromThrowable:What's the difference between map and andThen?
What's the difference between map and andThen?
map transforms the Ok value, returning a new Result:andThen is for chaining operations that can fail:map when the transformation cannot fail, and andThen when it can.How do I handle multiple Results at once?
How do I handle multiple Results at once?
Result.combine or Result.combineWithAllErrors:combine (short-circuits on first error):combineWithAllErrors (collects all errors):What is safeTry and when should I use it?
What is safeTry and when should I use it?
safeTry reduces boilerplate when working with multiple Results in sequence. It uses generator functions to implicitly return early on errors:Without safeTry:.safeUnwrap() anymore.Advanced Patterns
How do I perform side effects without affecting the Result?
How do I perform side effects without affecting the Result?
andTee for Ok values or orTee for Err values:Logging success without changing the Result:What's the difference between andTee and andThrough?
What's the difference between andTee and andThrough?
andTee: Side effects, errors are ignored:andThrough: Validation, errors propagate:How do I transition between sync and async Results?
How do I transition between sync and async Results?
asyncAndThen or asyncMap:Can I use NeverThrow with existing Promise-based code?
Can I use NeverThrow with existing Promise-based code?
ResultAsync.fromPromise:Testing
How do I test functions that return Results?
How do I test functions that return Results?
_unsafeUnwrap (recommended for tests):Should I enable the ESLint plugin in test files?
Should I enable the ESLint plugin in test files?
_unsafeUnwrap is legitimate in tests:Migration and Integration
How do I migrate an existing codebase to use NeverThrow?
How do I migrate an existing codebase to use NeverThrow?
- Start with new code: Write all new functions using Results
- Wrap throwing functions: Use
Result.fromThrowablefor existing functions - Update critical paths: Convert error-prone code paths first
- Add ESLint plugin: Enforce Result handling in new code
- Gradually refactor: Convert remaining code over time
Can I use NeverThrow with Express/Fastify/other frameworks?
Can I use NeverThrow with Express/Fastify/other frameworks?
What about using Results with React/Vue/other UI frameworks?
What about using Results with React/Vue/other UI frameworks?
Troubleshooting
Why am I getting 'cannot find module neverthrow' errors?
Why am I getting 'cannot find module neverthrow' errors?
Type inference isn't working correctly. What should I do?
Type inference isn't working correctly. What should I do?
- Updating TypeScript: NeverThrow requires TypeScript 4.7+
- Checking return types: Ensure functions explicitly return
Result<T, E> - Using explicit type annotations when needed:
Where can I get help or report issues?
Where can I get help or report issues?
- GitHub Issues: Report bugs or request features
- GitHub Discussions: Ask questions and share ideas
- Wiki: Check the wiki for guides
- Stack Overflow: Tag questions with
neverthrow
Additional Resources
- Migration Guide - Upgrade between versions
- TypeScript Tips - Advanced TypeScript patterns
- ESLint Plugin - Enforce proper Result handling
- GitHub Repository - Source code and examples