Error handling
Use Result for expected failures. Panic errors and argument validation errors indicate bugs in the calling code.
Panics
The following methods throw an internal PanicError. On async wrappers, they return a promise that rejects with that error:
| Method | Panics when |
|---|---|
Result.unwrap()Option.unwrap()AsyncResult.unwrap()AsyncOption.unwrap() | the outcome is Err or None |
Result.expect()Option.expect()AsyncResult.expect()AsyncOption.expect() | the outcome is Err or None |
Result.unwrapErr()AsyncResult.unwrapErr() | the value is Ok |
Result.expectErr()AsyncResult.expectErr() | the value is Ok |
Use these methods only when an Err or None would indicate a bug. For expected failures or absent values, use a fallback or handle each variant:
Result.unwrapOr(fallback)andOption.unwrapOr(fallback)return a default value.Result.unwrapOrElse((err) => fallback)andOption.unwrapOrElse(() => fallback)compute a default only when needed.Result.match({ Ok, Err })andOption.match({ Some, None })call a handler for the current variant.
import { Ok, Err } from 'results-ts';
// Return a fallback on Err.
const n = Ok(1).unwrapOr(0); // 1
const m = Err('oops').unwrapOr(0); // 0
// Throw on Err.
const k = Ok(1).unwrap(); // 1
const bad = Err('oops').unwrap(); // throws PanicErrorMisuse errors
Methods that accept callbacks or operands validate their types at runtime. Invalid arguments throw errors, including when untyped JavaScript or a type assertion bypasses TypeScript's checks. Fix the calling code when validation fails.
Error classes are not exported
The library does not export PanicError, InvalidArgumentError, or its other internal error classes. Use Result for expected failures and Option for absent values.
Exception adapters
Avoid catchUnwind and catchUnwindAsync. Use either only as a last resort when an external dependency or platform API inevitably throws or rejects, you cannot change that behavior, and no nonthrowing or Result-returning API is available. Some Node APIs require this exception.
Do not use these adapters for code you control, routine failures, general try/catch, or just to create a Result or AsyncResult. Return Err for recoverable failures in your own code, including invalid input and HTTP status failures. Keep any unavoidable adapter at the external call boundary, and use its onThrow handler to map the unknown thrown value to a specific error type.
For reference, this is how to adapt a legacy boundary that must keep a throwing JSON.parse call:
import { catchUnwind } from 'results-ts';
const parseLegacyJson = catchUnwind(
(text: string): unknown => JSON.parse(text),
(thrown) => (thrown instanceof Error ? thrown.message : 'Invalid JSON')
);How this differs from Rust
Rust uses a match expression. In TypeScript, call Result.match() or Option.match() with a handler for each variant. The constructors and panic methods use Rust's names. See the API reference for this library's behavior and the Rust documentation for Rust's types.
Next steps
- Async support explains how to chain and await async operations.
- API reference lists method signatures and behavior.