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

@trylonai/ts-result

v1.0.1

Published

Rust's Result type for type-safe error handling in TypeScript.

Readme

ts-result

npm version

ts-result provides a TypeScript implementation of the Result<T, E> type, commonly found in languages like Rust. It offers a mechanism for functions to return and propagate errors explicitly, without relying on exceptions or ambiguous return values like null or undefined.

Core Concept

A Result<T, E> is a discriminated union type representing one of two possibilities:

  1. Ok<T, E>: Represents success and contains a value of type T.
  2. Err<T, E>: Represents failure and contains an error value of type E.

This library provides the Result<T, E> type alias, the underlying Ok and Err classes (though direct use is typically via factory functions), and a set of methods to interact with these types in a type-safe manner.

Instances are created using the factory functions:

  • Ok(value: T): Creates a success result containing value.
  • Err(error: E): Creates an error result containing error.

Motivation

Traditional error handling in JavaScript/TypeScript often uses:

  • Exceptions (throw/try...catch): Implicitly alters control flow and requires callers to know when to catch. Errors are not part of the function signature.
  • null/undefined returns: Ambiguous; doesn't distinguish between a valid "not found" state and an actual error. Type checking doesn't enforce handling.

Result addresses this by making potential errors an explicit part of the function's return type (() => Result<User, FetchError>). This forces callers to acknowledge and handle potential failures, leveraging the TypeScript compiler for verification.

Features

  • Explicit Error Handling: Functions explicitly declare their potential failure modes via the Result<T, E> return type.
  • Type Safety: Utilizes TypeScript's discriminated unions and type guards (isOk, isErr) for compile-time verification of error handling paths.
  • Method-Based Operations: Provides methods for transforming (map, mapErr), chaining (andThen, orElse), and extracting values (unwrapOr, unwrapOrElse, match) without manual type checks in many cases.
  • Rust Result Parity: Implements core methods found on Rust's std::result::Result (see API).

Installation

npm install @trylonai/ts-result
# or
yarn add @trylonai/ts-result
# or
pnpm add @trylonai/ts-result

Basic Usage

import { Ok, Err, Result } from '@trylonai/ts-result';

// Function returning a Result
function divide(
  numerator: number,
  denominator: number
): Result<number, string> {
  if (denominator === 0) {
    return Err('Division by zero'); // Return an Err variant
  }
  return Ok(numerator / denominator); // Return an Ok variant
}

const result1 = divide(10, 2); // Ok(5)
const result2 = divide(5, 0); // Err("Division by zero")

// 1. Checking the variant using type guards
if (result1.isOk()) {
  // result1 is narrowed to Ok<number, string> here
  console.log('Result 1 is Ok:', result1.value); // Output: Result 1 is Ok: 5
}

if (result2.isErr()) {
  // result2 is narrowed to Err<number, string> here
  console.error('Result 2 is Err:', result2.error); // Output: Result 2 is Err: Division by zero
}

// 2. Handling both cases with match
const message = result1.match({
  Ok: (value) => `Success: ${value}`,
  Err: (error) => `Failure: ${error}`,
});
console.log(message); // Output: Success: 5

// 3. Extracting the value safely
const value1 = result1.unwrapOr(0); // Extracts 5 from Ok(5)
const value2 = result2.unwrapOr(0); // Returns default 0 because result2 is Err
console.log(value1, value2); // Output: 5 0

// 4. Chaining operations
function checkPositive(n: number): Result<number, string> {
  return n > 0 ? Ok(n) : Err('Number is not positive');
}

const positiveResult = divide(20, 4) // Ok(5)
  .andThen(checkPositive); // Calls checkPositive(5) -> Ok(5)

const nonPositiveResult = divide(-10, 2) // Ok(-5)
  .andThen(checkPositive); // Calls checkPositive(-5) -> Err("Number is not positive")

const errorResult = divide(10, 0) // Err("Division by zero")
  .andThen(checkPositive); // Does not call checkPositive, remains Err("Division by zero")

console.log(positiveResult); // Ok { value: 5 }
console.log(nonPositiveResult); // Err { error: 'Number is not positive' }
console.log(errorResult); // Err { error: 'Division by zero' }

// 5. Unwrapping (Use cautiously - throws on Err)
try {
  const unwrapped = result2.unwrap('Should have been Ok'); // Throws UnwrapError
} catch (e) {
  if (e instanceof UnwrapError) {
    console.error(e.message); // Output: Should have been Ok
    console.error(e.causeValue); // Output: Division by zero
  }
}

API Overview

Result<T, E> provides methods for:

  • Checking: isOk(), isErr() (act as type guards)
  • Extracting: unwrap(), unwrapErr() (throw on incorrect variant), unwrapOr(), unwrapOrElse() (provide defaults)
  • Transforming: map(), mapErr(), mapOr(), mapOrElse()
  • Chaining: andThen(), orElse()
  • Boolean Logic: and(), or()
  • Matching: match()