super-result
v1.0.0
Published
Lightweight railway-oriented error handling for TypeScript — discriminated Result<T,E>, typed async, zero unsafe casts, composable logging & tracing.
Downloads
728
Maintainers
Readme
super-result
Lightweight Result pattern for neater error handling written in TypeScript. Minimal syntax, maximum type safety.
📖 Philosophy
super-result is follows the Result pattern approach some know as Rust-inspired error handling. Read our Philosophy to learn more about it.
📦 Installation
pnpm add super-resultQuick Start
import { from } from 'super-result'
// Capture synchronous errors
const res1 = from(() => {
if (Math.random() > 0.5) throw new Error('boom')
return 42
})
// Capture asynchronous errors
const res2 = await from(async () => {
const data = await fetch('...')
return data.json()
})
// Type narrowing
if (res1.ok) {
console.log(res1.value)
} else {
console.error(res1.error.message)
}Custom Factories
import { createResult } from 'super-result'
class MyError extends Error {}
const coolResult = createResult(error =>
error instanceof MyError ? error : new MyError(String(error))
)
const result = coolResult.from(() => { throw new Error('raw') }) // no need to provide the error mapper anymore
// result.error is MyErrorAPI Reference
Full API documentation is available in the docs folder.
License
MIT © simwai
