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

matchable

v1.1.0

Published

A utility to define and match on tagged unions (like enums with payloads) — safely.

Readme

matchable

GitHub Actions version downloads semantic-release

A utility to define and match on tagged unions (like enums with payloads) — safely.


Why matchable?

Rust-style enums or Elm-style case matching in TypeScript.

Tagged unions with pattern matching — safely typed and runtime checked.

  • Exhaustive matching (TypeScript will catch missing cases)
  • Inferred payload types
  • One function: matchable()
  • No runtime dependencies

Installation

pnpm add matchable

Basic Usage

import { matchable } from "matchable";

const Result = matchable({
  Success: (value: number) => ({ value }),
  Failure: (message: string) => ({ message }),
});

const res = Result.Success(42);

const msg = Result.match(res, {
  Success: ({ value }) => `✅ ${value}`,
  Failure: ({ message }) => `❌ ${message}`,
  // ↑ TS ensures you cover every case
});

You must handle every variant — unless you provide a default.


Non-Exhaustive Matching

For partial matches, use a default fallback:

const msg = Result.match(Result.Failure("fail"), {
  Success: ({ value }) => `Success: ${value}`,
  default: () => "Something else happened",
});

Type Guards

Use .is helpers to narrow values:

const value: unknown = getResultSomehow();

if (Result.is.Success(value)) {
  console.log(value.value); // value: number
}

if (Result.is.Failure(value)) {
  console.error(value.message); // message: string
}

You can also validate that a value has a valid tag using .is.Valid():

if (Result.is.Valid(value)) {
  console.log("Tag is valid:", value.tag);
}

Useful in reducers, guards, conditionals, or when validating external data.


Runtime Safety

Unknown tags without a fallback will throw:

Result.match({ tag: "Missing" } as any, {
  Success: () => "",
  Failure: () => "",
});

// Error: Unhandled tag: Missing

This only happens when bypassing the type system (e.g. using as any).


Introspection

Check supported tags with _tags:

Result._tags;
// => ["Success", "Failure"]

Useful for debugging, docs, or introspective tooling.


Inferring the Union Type

Extract the full union type:

import type { MatchableOf } from "matchable";

const Result = matchable({
  Success: (value: number) => ({ value }),
  Failure: (message: string) => ({ message }),
});

type ResultType = MatchableOf<typeof Result>;
// => { tag: "Success"; value: number } | { tag: "Failure"; message: string }

Extract just the possible tag values:

import type { TagsOf } from "matchable";

type ResultTags = TagsOf<typeof Result>;
// => "Success" | "Failure"

This is helpful when you want to work with just the discriminant tags, such as in analytics, testing helpers, or editor tooling.


Group Match Handlers

Use group() to organize match handlers that share logic across tags — or to add a fallback handler.

import { group } from "matchable";

const Status = matchable({
  Idle: () => ({}),
  Loading: () => ({}),
  Success: (data: string) => ({ data }),
  Failure: (message: string) => ({ message }),
});

const log = (status: MatchableOf<typeof Status>) => {
  console.log("handling", status.tag);
};

const handlers = group(Status, {
  Idle: log,
  Loading: log,
  default: (status) => console.log("fallback", status.tag),
});

Status.match(Status.Idle(), handlers); // logs: handling Idle
Status.match(Status.Loading(), handlers); // logs: handling Loading
Status.match(Status.Success("done"), handlers); // logs: fallback Success

You can still handle specific cases:

const handlers = group(Status, {
  Success: (val) => console.log("✅", val),
  Failure: (val) => console.error("❌", val),
  default: () => console.log("Something else"),
});

Status.match(Status.Loading(), handlers); // logs: Something else

Serialize and Deserialize

You can serialize a variant to a JSON string — and safely deserialize it back later:

const original = Result.Success(42);

const json = Result.serialize(original);
// => '{"tag":"Success","value":42}'

const parsed = Result.deserialize(json);
// => { tag: "Success", value: 42 }

if (Result.is.Success(parsed)) {
  console.log(parsed.value); // 42
}

Useful when persisting tagged data to localStorage, sending it over the wire, or hydrating server responses.

Note: Deserialized objects do not include internal metadata like __matchable_id__, but still pass is.Valid() and work with match().


Credits

Inspired by:


PRs and ideas welcome.