Skip to content

simwai/super-result

Repository files navigation

super-result

super-result

Lightweight, enterprise-grade railway-oriented error handling for TypeScript. Minimal syntax, maximum type safety.

TypeScript Node License: MIT


📦 Installation

pnpm add super-result

Quick Start

import { from, ok, err } 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 R = createResult(error =>
  error instanceof MyError ? error : new MyError(String(error))
)

const res = R.from(() => { throw new Error('raw') })
// res.error is guaranteed to be MyError

API Reference

Full API documentation is available in the docs folder.


License

MIT © simwai

Contributors