A lightweight, zero-dependency Result type inspired by Rust. Handle success and failure with total type safety, async pipelines, and zero runtime overhead.
The most complete Result implementation for the TypeScript ecosystem.
Force explicit handling of success and error cases. No more try/catch blocks leaking into your business logic.
Extremely lightweight with zero dependencies. Tree-shakeable and optimized for both browser and Node.js (18+).
Native ResultAsync support for chaining asynchronous operations without nested await statements.
npm install @davemanufor/resultifyRequires Node.js 18+ and TypeScript 4.5+. Works in all modern browsers.
| Import | Use when |
|---|---|
| @davemanufor/resultify | Sync Result, Ok, Err, guards |
| @davemanufor/resultify/async | ResultAsync pipelines |
| @davemanufor/resultify/jest | Jest custom matchers (dev/test only) |
The core of Resultify is the Result type. It represents either a success (Ok) or a failure (Err).
Creates a success (Ok) result containing the provided value. Fully infers the type of the value.
Creates a failure (Err) result containing the provided error. Errors can be strings, plain objects, or discriminated unions.
Safely executes a synchronous function that might throw. Returns an Ok with the return value, or an Err wrapping the caught exception.
Converts a potentially null or undefined value into a Result. If the value is null/undefined, returns the provided error.
Combines an array of Results. Short-circuits and returns the first Err it encounters, otherwise returns an Ok containing an array of values.
Collects the outcomes of all results without short-circuiting. Similar to Promise.allSettled.
Returns the first Ok result encountered. If all results are Errs, returns an Err containing an array of all the errors.
Splits an array of mixed results into a tuple of [oks, errs], extracting their inner values.
Wraps an asynchronous function, returning a Promise that resolves to a Result. Catch mapped implicitly.
Reconstructs a Result instance from the serialized output of toJSON().
Stop the "await-hell". Chain asynchronous operations as easily as synchronous ones. ResultAsync mirrors almost the entire synchronous Result API but returns Promises internally.
Concurrently awaits multiple ResultAsync operations, failing fast if any return an Err. Similar to Promise.all but for Results.
Awaits the internal pipeline and returns a synchronous Result. You must call this (or match) to resolve the chain.
Resultify's greatest strength is its ability to handle any error type—not just Error objects. Use discriminated unions for rich, domain-specific error handling.
Make your tests more readable with custom matchers. No more expect(res.isOk()).toBe(true).
toBeOk()toBeErr()toBeOkWith(value)toBeErrWith(error)