Skip to content

mastrojs/result

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@mastrojs/result

An extremely simple Result type. Return either an Ok or Err instead of throwing an exception.

Exceptions in TypeScript are not type-safe, and it's often hard to know whether a function can throw or not. For an introduction of the concept, see e.g. Using Results in TypeScript.

Inspired by Rust's Result<T, E> type (known as Either in some functional languages), this implementation is even more minimal than ts-results (which is also unmaintained). If you're looking for a more object-oriented alternative, see neverthrow. Unlike the [data, error] approach (used e.g. in Go), you don't risk getting the tuple index wrong.

Usage

import { Ok, Err, type Result } from "@mastrojs/result";

const go = (): Result<number> =>
  Math.random()
    ? Ok(3)
    : Err("Oh noes");

const res = go();
if (res.ok) {
  console.info(res.val);
} else {
  console.error(res.error);
}

To see all functions @mastrojs/result exports, see its API docs.

The string literal in AppError.error can be used to keep track of known errors:

// e has type `AppError<"timeout" | "crash">`
const e = Math.random() ? Err("timeout") : Err("crash");

Result<T, E> is generic in both arguments. E defaults to AppError, but you can also use a custom error type (as long as it has a field ok: undefined), in which case you have to bring your own CustomErr constructor.

Install

Deno

deno add jsr:@mastrojs/result

Node.js

pnpm add jsr:@mastrojs/result

Bun

bunx jsr add @mastrojs/result

About

A minimal TypeScript Result type

Topics

Resources

License

Stars

Watchers

Forks

Contributors