npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@usersatoshi/results

v1.0.0

Published

A Result type implementation for TypeScript

Readme

@usersatoshi/results

A lightweight TypeScript Result type for safe error handling. Write predictable, composable code without throwing exceptions.

Features

  • Lightweight - Zero dependencies, minimal bundle size
  • Type-safe - Full TypeScript support with discriminated unions
  • Runtime-agnostic - Works with Node.js, Bun, Deno, and browsers
  • Composable - Chain operations with map, andThen, and more
  • Async support - Convert promises to Results with fromPromise

Installation

npm install @usersatoshi/results

Or with your preferred package manager:

bun add @usersatoshi/results
yarn add @usersatoshi/results
pnpm add @usersatoshi/results

Quick Start

import { ok, err, type Result } from '@usersatoshi/results';

const enum ErrorKind { NotFound = 0, Unauthorized = 1 }
type AppError = { kind: ErrorKind };

// Create an Ok result
const success: Result<number, AppError> = ok(42);

// Create an Err result
const failure: Result<number, AppError> = err({ kind: ErrorKind.NotFound });

// Use type guards (methods, not properties)
if (success.isOk()) {
  console.log(success.value); // 42
}

if (failure.isErr()) {
  console.log(failure.error); // { kind: 0 }
}

API

ok<T>(value: T): Ok<T>

Creates an Ok result wrapping a successful value.

const result = ok(100);

err<E extends { kind: number }>(error: E): Err<E>

Creates an Err result wrapping an error.

const result = err({ kind: 1 as const, message: "Something went wrong" });

fromPromise<T, E>(promise: Promise<T>, onErr: (error: unknown) => E): Promise<Result<T, E>>

Converts a Promise into a Result, mapping rejections with your error handler.

const result = await fromPromise(
  fetch('/api/data'),
  (error) => ({ kind: 0 as const, message: String(error) })
);

Result Methods

Both Ok and Err implement the Result interface with methods for chaining operations:

  • isOk() - Type guard: narrows to Ok<T>
  • isErr() - Type guard: narrows to Err<E>
  • map(fn) - Transform the success value; propagates Err unchanged
  • mapErr(fn) - Transform the error value; propagates Ok unchanged
  • andThen(fn) - Chain Result-returning operations (flatMap)
  • orElse(fn) - Recover from an error with a new Result
  • match(onOk, onErr) - Exhaustively handle both variants
  • unwrap() - Extract the value, throws ResultError if Err
  • unwrapOr(default) - Extract the value or return a default
  • BaseResult.all(results) - Collect array of Results; returns first Err or Ok<T[]>
  • BaseResult.any(results) - Return first Ok, or last Err if all fail

Examples

Error Handling

const enum ErrorKind { DivisionByZero = 0 }
type MathError = { kind: ErrorKind };

function divide(a: number, b: number): Result<number, MathError> {
  if (b === 0) {
    return err({ kind: ErrorKind.DivisionByZero });
  }
  return ok(a / b);
}

const result = divide(10, 2);

if (result.isOk()) {
  console.log(`Result: ${result.value}`); // Result: 5
}

Chaining Operations

ok(5)
  .map(x => x * 2)
  .andThen(x => x > 5 ? ok(x) : err({ kind: 0 as const }))
  .match(
    (value) => console.log(`Success: ${value}`),
    (error) => console.log(`Error: ${error.kind}`)
  );

Async Operations

const result = await fromPromise(
  fetch('/api/users'),
  (error) => ({ kind: 1 as const, message: String(error) })
);

if (result.isOk()) {
  const users = await result.value.json();
  console.log(users);
}

Collecting Multiple Results

const results = [ok(1), ok(2), ok(3)];
const combined = BaseResult.all(results);

if (combined.isOk()) {
  console.log(combined.value); // [1, 2, 3]
}

Browser Compatibility

Works in all modern browsers that support ES2020+.

License

Apache-2.0

Repository

  • Main: https://git.usersatoshi.com/usersatoshi/results.git
  • Mirror: https://github.com/usersatoshi/results