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

neverthrow-pattern

v0.1.0

Published

<h1 align="center">Neverthrow-Pattern</h1>

Downloads

3

Readme

Description

neverthrow-pattern bridges the gap between two powerful TypeScript libraries: neverthrow and ts-pattern. It provides a seamless integration layer that allows you to use pattern matching with the Result type from neverthrow, making your error handling code more expressive, concise, and type-safe.

import { match } from "ts-pattern";
import { NP } from "neverthrow-pattern";
import { Result, ok, err } from "neverthrow";

const handleResult = (result: Result<number, string>) =>
    match(result)
        .with(NP.ok(), (value) => `Success: ${value.value}`)
        .with(NP.ok(42), () => "The answer is 42!")
        .with(NP.err(P.string), (error) => `Error: ${error.value}`)
        .exhaustive();

// Usage
handleResult(ok(42)); // "The answer is 42!"
handleResult(ok(7)); // "Success: 7"
handleResult(err("Something went wrong")); // "Error: Something went wrong"

Features

  • 🎯 Pattern match on neverthrow's Result type: Use expressive pattern matching for both Ok and Err values
  • 🛡️ Type-safe: Full type inference and exhaustiveness checking
  • 🧩 Composable patterns: Combine with other ts-pattern patterns for complex matching scenarios
  • 🔍 Pattern validation: Match on the inner value of Ok and Err instances
  • 📝 Comprehensive type definitions: Works seamlessly with TypeScript
  • 🔄 Match type interoperability: Enhances the standard match function to work with neverthrow Results

Installation

# Using pnpm
pnpm add neverthrow-pattern

# Using yarn
yarn add neverthrow-pattern

# Using npm
npm install neverthrow-pattern

Make sure you have both neverthrow and ts-pattern installed:

pnpm add neverthrow ts-pattern

Usage

Importing

// Import everything under a namespace (recommended)
import { NP } from "neverthrow-pattern";

// Import match and other utilities from ts-pattern
import { match, P } from "ts-pattern";

// Import from neverthrow as usual
import { Result, ok, err } from "neverthrow";

Basic Pattern Matching on Result

Match on any Ok or Err value:

const processResult = (result: Result<number, string>) =>
    match(result)
        .with(NP.ok(), ({ value }) => `Got success with value: ${value}`)
        .with(NP.err(), ({ error }) => `Got error: ${error}`)
        .exhaustive();

Pattern Matching with Value Validation

Match on specific values inside Ok or Err:

const processApiResult = (
    result: Result<
        { status: "success" | "pending"; data: unknown },
        { code: number; message: string }
    >
) =>
    match(result)
        .with(
            NP.ok({ status: "success" }),
            ({ value }) => `Success: ${JSON.stringify(value.data)}`
        )
        .with(NP.ok({ status: "pending" }), () => "Operation in progress...")
        .with(NP.err({ code: 404 }), () => "Not found")
        .with(
            NP.err({ code: P.number.gt(500) }),
            ({ error }) => `Server error: ${error.message}`
        )
        .with(NP.err({ code: P.number }), ({ error }) => `Error: ${error}`)
        .exhaustive();

Using with ts-pattern's Other Patterns

Combine with other patterns from ts-pattern:

match(result)
    .with(NP.ok(P.when((n) => n > 10)), () => "Got a number greater than 10")
    .with(NP.ok(), ({ value }) => `Got: ${value}`)
    .with(NP.err(P.string), ({ error }) => `Error message: ${error}`)
    .with(NP.err(P.any), () => "Some other error")
    .exhaustive();

Pattern Selection

Note: Currently, using P.select() inside NP.ok() and NP.err() patterns is not supported. For example, NP.ok(P.select()) or NP.err(P.select()) won't work as expected. This feature may be implemented in a future version.

Enhanced Match Type Interoperability

The library enhances the standard match function to work better with neverthrow Results. When using neverthrow-pattern, the match function from ts-pattern is automatically enhanced to work better with Result types. This means:

  1. You don't need any special imports or setup - just import the library and start using it
  2. The type system correctly distributes union types within Results for precise pattern matching
  3. Type inference works properly when destructuring Ok and Err values

API Documentation

Main Functions

  • NP.ok<input, pattern>(pattern?): Creates a pattern that matches Ok values
  • NP.err<input, pattern>(pattern?): Creates a pattern that matches Err values

Both functions can be used:

  • Without arguments to match any Ok/Err value
  • With a pattern to match against the inner value

Type Definitions

  • UnwrapOk<T>: Extracts the success type from a Result
  • UnwrapErr<T>: Extracts the error type from a Result

For complete API documentation, please refer to the generated TypeDoc documentation.

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch: git checkout -b my-new-feature
  3. Make your changes
  4. Create a changeset to document your changes: pnpm changeset
  5. Commit your changes: git commit -am 'Add some feature'
  6. Push to the branch: git push origin my-new-feature
  7. Submit a pull request

Please make sure your code follows the existing style and includes appropriate tests.

For more detailed information, see the CONTRIBUTING.md guide.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • neverthrow - A type-safe alternative to throwing exceptions
  • ts-pattern - The exhaustive Pattern Matching library for TypeScript
  • All contributors who have helped improve this library

Built with ❤️ for type-safe error handling in TypeScript.