Result<T, E> represents either a success (Ok<T>) or a failure (Err<E>). Unlike try/catch, errors appear directly in the function signature, so TypeScript enforces that you handle them.
Creating results
Wrapping existing code
UsetryCatch to safely call any function that might throw:
fromPromise to wrap a Promise that might reject:
Chaining
All chaining methods skip the transformation when the result is anErr, so errors short-circuit automatically.
.map() and .mapErr()
Transform the value or the error without unwrapping:
.andThen()
Chain a function that itself returns a Result. Use this when the next step can also fail:
.orElse()
Recover from an error by returning a fallback Result:
Side effects
Use.tap() and .tapErr() to run side effects without modifying the result:
Consuming results
.match()
The cleanest way to handle both branches:
Type narrowing with .isOk() / .isErr()
.unwrap() and .unwrapOr()
.unwrap() throws if the result is an Err. Use it only when you are certain the result is Ok, or as a last resort:
Attaching debug context
Use.context() to annotate errors with information about where in the chain the failure occurred. Context accumulates in order from most recent to oldest:
The built-in .pipe() method
Result has a .pipe() method that lets you pass the result through a sequence of functions:
ResultAsync is awaitable
ResultAsync<T, E> implements PromiseLike, so you can await it directly to get a synchronous Result<T, E>:
Combining multiple results
chas.shapeAsync()
Combine several ResultAsync values into a single shaped object. If any input is an Err, the first error is returned:
- Sync: chas.shape()
- Async: chas.shapeAsync()
Parallel combinators
Do-notation with chas.go()
Use chas.go() to write sequential Result-based code without deeply nested .andThen chains. Yielding an Err short-circuits immediately: