Skip to content

simwai/super-result

Repository files navigation

super-result

super-result

Lightweight Result pattern for neater error handling written in TypeScript. Minimal syntax, maximum type safety.

TypeScript Node License: MIT


📖 Philosophy

super-result follows the Result pattern approach some know as Rust-inspired error handling. Read our Philosophy to learn more about it.


📦 Installation

pnpm add super-result

Quick 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 MyError

API Reference

Full API documentation is available in the docs folder.


License

MIT © simwai