

TypeScript Error Handling
A fully typed error handling library for TypeScript, inspired by Rust's Result type and neverthrow. This library provides a type-safe way to handle errors without throwing exceptions, enabling railway-oriented programming patterns.
Features
- Fully Typed: Leverage TypeScript's type system for complete type safety
- Narrow Type Guards: Automatic type narrowing with
isOk and isErr
- Zero Dependencies: Lightweight and self-contained
- Rust-Inspired API: Familiar patterns from Rust's Result type
- Railway-Oriented Programming: Chain operations safely with
map, andThen, and more
- Async Support: First-class support for async operations
- Comprehensive Utilities: Combine, traverse, sequence, and more
Installation
bun add ts-error-handling
npm install ts-error-handling
pnpm add ts-error-handling
Quick Start
import type { Result } from 'ts-error-handling'
import { err, ok } from 'ts-error-handling'
function divide(a: number, b: number): Result<number, string> {
if (b === 0) {
return err('Division by zero')
}
return ok(a / b)
}
const result = divide(10, 2)
if (result.isOk) {
console.log(result.value)
}
else {
console.log(result.error)
}
Core API
Creating Results
import { err, ok } from 'ts-error-handling'
const success = ok(42)
const failure = err('error')
Type Narrowing
const result: Result<number, string> = ok(42)
if (result.isOk) {
console.log(result.value)
}
else {
console.log(result.error)
}
Transforming Results
ok(21).map(x => x * 2)
err('failed').map(x => x * 2)
ok(42).mapErr(e => e.toUpperCase())
err('failed').mapErr(e => e.toUpperCase())
ok(21)
.andThen(x => ok(x * 2))
.andThen(x => x > 40 ? ok(x) : err('too small'))
err('failed')
.orElse(e => ok(0))
Pattern Matching
const message = result.match({
ok: value => `Success: ${value}`,
err: error => `Error: ${error}`
})
Unwrapping Values
ok(42).unwrap()
err('failed').unwrap()
ok(42).unwrapOr(0)
err('failed').unwrapOr(0)
ok(42).unwrapOrElse(e => 0)
err('failed').unwrapOrElse(e => 0)
ok(42).expect('should work')
err('failed').expect('should work')
Utility Functions
Exception Handling
import { fromPromise, tryCatch, tryCatchAsync } from 'ts-error-handling'
const result = tryCatch(
() => JSON.parse(jsonString),
error => `Parse error: ${error}`
)
const asyncResult = await tryCatchAsync(
async () => await fetch('/api/data'),
error => new ApiError(error)
)
const promiseResult = await fromPromise(
fetch('/api/users/1'),
error => `Failed to fetch: ${error}`
)
Combining Results
import { all, combine, combineWithAllErrors } from 'ts-error-handling'
const result1 = combine([
validateEmail(email),
validatePassword(password),
validateAge(age)
])
const result2 = combineWithAllErrors([
validateEmail(email),
validatePassword(password),
validateAge(age)
])
const result3 = all([ok(1), ok(2), ok(3)])
Array Operations
import { partition, traverse, traverseAsync } from 'ts-error-handling'
const numbers = traverse(['1', '2', '3'], (s) => {
const n = Number.parseInt(s)
return Number.isNaN(n) ? err('Invalid') : ok(n)
})
const users = await traverseAsync([1, 2, 3], async (id) => {
return fetchUser(id)
})
const results = [ok(1), err('error'), ok(2)]
const [successes, failures] = partition(results)
Working with Nullable Values
import { fromNullable } from 'ts-error-handling'
interface Config {
apiKey?: string
}
function getApiKey(config: Config): Result<string, string> {
return fromNullable(config.apiKey, 'API key is required')
}
Advanced Utilities
import {
any,
filter,
flatten,
getOrElse,
swap,
tap,
toPromise,
unwrapOrThrow
} from 'ts-error-handling'
const nested: Result<Result<number, string>, string> = ok(ok(42))
const flat = flatten(nested)
const logged = tap(
result,
value => console.log('Success:', value),
error => console.error('Error:', error)
)
const swapped = swap(ok(42))
const swapped2 = swap(err('failed'))
const filtered = filter(
ok(42),
n => n > 0,
'must be positive'
)
const value = unwrapOrThrow(result, e => new Error(e))
const promise = toPromise(result)
const value2 = getOrElse(result, e => defaultValue)
const first = any([err('e1'), ok(42), err('e2')])
Async Operations
import { parallel, sequence } from 'ts-error-handling'
const sequential = await sequence([
async () => fetchUser(1),
async () => fetchUser(2),
async () => fetchUser(3)
])
const parallelResult = await parallel([
async () => fetchUser(1),
async () => fetchUser(2),
async () => fetchUser(3)
])
Why Use Result Types?
- Explicit Error Handling: Errors are part of the function signature
- No Hidden Control Flow: No try/catch blocks, exceptions are values
- Type Safety: The compiler enforces error handling
- Composability: Chain operations without nested error handling
- Self-Documenting: Function signatures show what can fail and how
Examples
See the examples.ts file for comprehensive examples including:
- Async operations
- Form validation with error collection
- Railway-oriented programming
- API client implementation
- Array operations with
traverse
- Sequential and parallel execution
Complete API Reference
Core Functions
ok<T, E>(value: T) | Creates a successful Result |
err<T, E>(error: E) | Creates a failed Result |
isResult<T, E>(value: unknown) | Type guard to check if value is a Result |
Result Methods
.map<U>(fn: (value: T) => U) | Transform the Ok value |
.mapErr<F>(fn: (error: E) => F) | Transform the Err value |
.andThen<U>(fn: (value: T) => Result<U, E>) | Chain Result-returning operations |
.orElse<F>(fn: (error: E) => Result<T, F>) | Recover from errors |
.match<U>(patterns: {...}) | Pattern matching on Ok/Err |
.unwrap() | Get value or throw |
.unwrapOr(defaultValue: T) | Get value or return default |
.unwrapOrElse(fn: (error: E) => T) | Get value or compute default |
.expect(msg: string) | Get value or throw with custom message |
Exception Handling
tryCatch<T, E>(fn, errorHandler?) | Wrap function that might throw |
tryCatchAsync<T, E>(fn, errorHandler?) | Wrap async function that might throw |
fromPromise<T, E>(promise, errorHandler?) | Convert Promise to Result |
Combining Results
combine<T[]>(results) | Combine Results, stop at first error |
combineWithAllErrors<T[]>(results) | Combine Results, collect all errors |
all<T, E>(results) | Collect all Ok values or return first Err |
any<T, E>(results) | Return first Ok or last Err |
Array Operations
traverse<T, U, E>(items, fn) | Map over array with Result-returning function |
traverseAsync<T, U, E>(items, fn) | Async version of traverse |
partition<T, E>(results) | Split Results into [successes, failures] |
Async Operations
sequence<T, E>(operations) | Execute async operations sequentially |
parallel<T, E>(operations) | Execute async operations in parallel |
Transformations
flatten<T, E>(result) | Flatten nested Result<Result<T, E>, E> |
swap<T, E>(result) | Swap Ok and Err values |
filter<T, E>(result, predicate, error) | Filter Result based on predicate |
Utilities
fromNullable<T, E>(value, error) | Convert nullable to Result |
tap<T, E>(result, onOk?, onErr?) | Side effects without modifying Result |
unwrapOrThrow<T, E>(result, mapError?) | Convert to exception at boundaries |
toPromise<T, E>(result, mapError?) | Convert Result to Promise |
getOrElse<T, E>(result, fn) | Get value or compute default |
Type Utilities
Result<T, E> | Union of Ok<T, E> and Err<T, E> |
ResultAsync<T, E> | Promise<Result<T, E>> |
InferOkType<T> | Extract Ok type from Result |
InferErrType<T> | Extract Err type from Result |
Testing
bun test
Changelog
Please see our releases page for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
Discussions on GitHub
For casual chit-chat with others using this package:
Join the Stacks Discord Server
Postcardware
“Software that is free, but hopes for a postcard.” We love receiving postcards from around the world showing where Stacks is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
License
The MIT License (MIT). Please see LICENSE for more information.
Made with 💙