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

ts-result-monad

v1.0.1

Published

Rust-style Result<T, E> for TypeScript — type-safe error handling without try/catch

Readme

ts-result — Rust-Style Result<T, E> for TypeScript

npm version npm downloads CI TypeScript license

Rust-style Result<T, E> for TypeScript — type-safe error handling where errors become part of the return type, not invisible exceptions. ~900 bytes, zero dependencies.

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) return Err("division by zero");
  return Ok(a / b);
}

const result = divide(10, 0);
if (result.ok)
  console.log(result.value); // typed as number
else console.log(result.error); // typed as string

ts-result demo — Ok and Err type narrowing with TypeScript autocomplete

Install

npm install ts-result-type

Usage

import { Ok, Err, Result } from "ts-result-type";

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) return Err("division by zero");
  return Ok(a / b);
}

const result = divide(10, 2);

if (result.ok) {
  console.log(result.value); // 5 — fully typed as number
} else {
  console.log(result.error); // fully typed as string
}

Pattern Matching

const message = Result.match(divide(10, 0), {
  ok: (value) => `Result: ${value}`,
  err: (error) => `Failed: ${error}`,
});
// "Failed: division by zero"

Chaining

const result = Result.flatMap(Ok("42"), (s) => {
  const n = parseInt(s, 10);
  return isNaN(n) ? Err("not a number") : Ok(n);
});
// Ok(42)

Wrapping Throwable Code

import { fromThrowable, fromPromise } from "ts-result-type";

const parsed = fromThrowable(() => JSON.parse(input));

const data = await fromPromise(
  fetch("/api/data").then((r) => r.json()),
  (e) => `Fetch failed: ${(e as Error).message}`,
);

API

Constructors

| Function | Description | | ------------------------------- | -------------------------------- | | Ok(value) | Create a success result | | Err(error) | Create an error result | | fromThrowable(fn, mapErr?) | Wrap a function that might throw | | fromPromise(promise, mapErr?) | Wrap a promise that might reject |

Static Methods

| Method | Description | | ----------------------------------- | --------------------------------- | | Result.map(result, fn) | Transform the Ok value | | Result.mapErr(result, fn) | Transform the Err value | | Result.flatMap(result, fn) | Chain Result-returning functions | | Result.unwrap(result) | Extract value or throw | | Result.unwrapOr(result, default) | Extract value or use default | | Result.unwrapErr(result) | Extract error or throw | | Result.match(result, { ok, err }) | Pattern match on result | | Result.isOk(result) | Type guard for Ok (narrows type) | | Result.isErr(result) | Type guard for Err (narrows type) |

Why Not Try/Catch?

  • Type safety — errors are part of the return type, not invisible
  • Exhaustive handling — TypeScript ensures you handle both cases
  • Composable — chain operations with map, flatMap, match
  • Explicit — no hidden control flow, no forgotten catch blocks

Author

Ofer Shapira

LinkedIn GitHub

License

MIT