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

rstypes

v1.1.0

Published

Type-safe implementation of Rust's Option and Result types

Downloads

129

Readme

RSTypes

Type-safe implementation of lightweight Option and Result types for TypeScript and JavaScript.

import { Option, Some, None } from "rstypes/option";

function findIndex<T>(array: T[], search: T): Option<number> {
    const index = array.indexOf(search);
    return index === -1 ? None : Some(index);
}

const idx = findIndex([1, 2, 3], 2);

idx.match({
    Some(value) { console.log("Found at index:", value) },
    None() { console.log("Not found") }
});
import { Result, Ok, Err } from "rstypes/result";

function parseIntSafe(str: string): Result<number, string> {
    const num = parseInt(str);
    return isNaN(num) ? Err("Invalid string") : Ok(num);
}

const result = parseIntSafe("333");
result.match({
    Ok(num) { console.log("Parsed:", num) },
    Err(msg) { console.error("Error:", msg) }
});

Table of Contents

Why is this useful?

rstypes can help you build clearer APIs for your libraries and code that allow for more explicit handling and type checking.

  • No more hidden null/undefined. Option provides a structured way to handle missing values rather than just finding an undefined where you expected a string.

  • Explicit error handling. Unlike exceptions, which can be ignored or go unnoticed, Result forces you to acknowledge and handle error cases at the type level. The type also explicitly encodes what errors the function will return, making APIs clear.

  • Extra type safety. TypeScript ensures that you've handled both success and failure branches when using match, and supports type narrowing on different cases, providing compile-time guarantees for your logic.

  • Fast, declarative operations. Transformations can be chained with map, or etc. which will automatically propagate the alternate cases. This keeps logic flat and readable and avoids a branching check for performance (the correct cases get called directly by each value object).

Usage

npm install rstypes

Basic Types

Option<T> Represents an optional value (which may be Some(value: T) or None). Use it where null or undefined would go: explicit optional parameters, nullable fields, or functions that might not return a result.

Result<T, E> Represents a success (Ok(value: T)) or a recoverable error (Err(error: E)). Use it for operations that can fail, like parsing or network requests.

Note: When making functions that return these values, take care to explicitly annotate return types as Option<T> or Result<T, E> to maximise clarity in your API. Typescript will by default infer the values as Some<T> | None and Ok<T> | Err<E>, which are equivalent in function but will have messier definitions for some methods.

Handling Values

In the below examples, consider example Option and Result variables

let opt: Option<number> = /* ... */
let res: Result<number, string> = /* ... */

Where number is the Ok/Some type and string is the Err type of the result.

match

Exhaustive pattern matching. Supports both verbose object syntax and concise functional syntax.

// Object syntax
const val opt.match({
    Some(value) { return value; }
    None() { return 0; }
})

res.match({
    Ok(value) { console.log(value) },
    Err(error) { console.error(error) }
});

// Functional syntax
opt.match(
    value => console.log(value),
    () => console.error("There was no value")
);

const val2 = res.match(
    value => value * 2,
    error => 0
);

unwrap & expect

Use only when an inner value certainly exists. Throws an error if called on None or Err. expect allows a custom message.

const val = opt.unwrap();
const val2 = res.expect("Data should be present");

the Result type has additional methods unwrap_err and expect_err for the error case.

unwrap_or & unwrap_or_else

Provide defaults when a value is missing or an error occurs. unwrap_or takes a value, unwrap_or_else takes a function.

const val = opt.unwrap_or(0);
const val2 = res.unwrap_or_else((err) => computeFallback(err));

Predicates

Check the variant and narrow types.

  • is_ok() / is_err() (Result)
  • is_some() / is_none() (Option)
if(res.is_err()) {
    // res gets narrowed to Err<string>, so calling unwrap() returns never!
}

Additional predicates can be used to verify a condition on the value, or evaluating to false on the wrong case.

  • is_some_and(predicate) / is_none_or(predicate) (Option)
  • is_ok_and(predicate) / is_err_and(predicate) (Result)
const greater_than_3 = opt.is_some_and(x => x > 3); // false if Some(x <= 3) or None

Mapping

Transform inner values while propagating None or Err.

const doubled = opt.map(n => n * 2);               // Option<number>
const wrappedErr = res.map_err(e => new Error(e)); // Result<number, Error>

the Result type has the additional method map_err for the error case.

Conversion

Move between Option and Result.

  • res.ok() converts Result<T, E> to Option<T>.
  • res.err() converts Result<T, E> to Option<E>.
  • opt.ok_or(err: E) converts Option<T> to Result<T, E>.

and, or, xor

Perform logical operations on the values.

  • res1.and(res2) / opt1.and(opt2): Evaluate to the alternative on the success case, or the first on the error case.
  • res1.or(res2) / opt1.or(opt2): Evaluate to the first on the success case, and the alternative on the error case.
  • opt1.xor(opt2): Evaluate to None if both or neither are None, and the only Some otherwise.

Utilities

Convert standard JS functions to return Option or Result:

  • as_result Wraps a throwing function.
  • as_option Wraps a function returning falsy values (like null, undefined or NaN).
import { as_result } from "rstypes/result";
const safeParse = as_result(JSON.parse);

import { as_option } from "rstypes/option";
const safeSqrt = as_option(Math.sqrt);

calls to as_option may also specify an additional predicate for when the returned value should be None, rather than on all falsy values.

// Concise implementation of index_in_array supplying a none predicate
const index_in_array = as_option(
    (array, search) => array.indexOf(search), 
    value => value === -1
);