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

switche

v1.1.0

Published

TypeScript-safe switch expression syntax

Readme

Switche

Have you ever wished TypeScript had an expression-syntax equivalent of switch? I have. Unfortunately, JavaScript/TypeScript switch doesn't return a value. You can fake it with a function, something like this:

const result = (() => {
    switch (value) {
        case 1: return 'One';
        case 2: return 'Two';
        default: return 'Neither'
    }
})();

That's a lot of code, though, and you need to add a return type annotation if you want some kind of enforcement of the return type (in the above example, you can change 'two' to 2 and TypeScript won't complain).

Switche gives you a nicer syntax to achieve the same result, with a little more type safety.

Value Syntax

Provide the value to test in a call to switche, then provide multiple .case calls, and finally a .default call. The result of the call chain will be the second parameter of the first matching case, or else the parameter passed to .default

const result = switche(value)
    .case(1, 'One')
    .case(2, 'Two')
    .default('Neither');

Predicate syntax

As well as providing values to match against, you can provide predicate functions:

const result = switche(value)
    .case(v => v < 0, 'Negative')
    .case(1, 'One')
    .case(2, 'Two')
    .default('Neither');

Throw if not matched

Instead of providing a default, you can tell Switche to throw an error if there is no match. .orThrow takes two optional parameters: an error message and an error type. If you provide an error type, it must inherit from Error and accept a string in the constructor.

const result = switche(value)
    .case(1, 'One')
    .orThrow('The provided value was not 1');
class CustomError extends Error {    
    constructor(details) {
        super(`Custom error being thrown with message '${details}'`);
    }
}
const result = switche(value)
    .case(1, 'One')
    .orThrow('The provided value was not 1', CustomError);

If your custom error constructor doesn't take a single string parameter or you otherwise want to control the creation of the error, you can pass a function as the first parameter which will be called to create the error to throw. In this case, the second parameter is ignored.

class ComplexCustomError extends Error {
    constructor(errorCode: number) {
        super(`Error thrown with code ${errorCode}`);
    }
}
try {
    const result = switche(i)
    .case(2, 'Two')
    .orThrow(() => new ComplexCustomError(5));
} catch (ex) {
    ex;//?
}

Type Safety

const result = switche(value)
    .case(1, 'One')
    .case(2, 'Two')
    .default('Neither'); // Expression type: string

Unlike in the switch example above, if you replace one of the strings with something of a different type, you'll get a type error. All to case or default must be of the same type as the first parameter to the first case call.

const result = switche(value)
    .case(1, 'One')
    .case(2, 2) // Type error: Argument of type 'number' is not assignable to parameter of type 'string'.
    .default('Neither');

You can control the result type using generic parameters. For example, if you want to allow either strings or numbers, you can provide string|number as a generic parameter to the first case call.

const result = switche(value)
    .case<string|number>(1, 'One')
    .case(2, 2)
    .default('Neither'); // Expression type: string|number

You can place the generic parameter immediately after the switche call as well, but then you have to provide the input type as the first parameter (the second parameter, which specifies the output type, is optional):

const result = switche<number,string|number>(value)
    .case(1, 'One')
    .case(2, 2)
    .default('Neither'); // Expression type: string|number

If you provide generic parameters in both places, the second one will be ignored. In this example, string (provided as the generic parameter to switche) will be used, and number|string provided to the first case call will be ignored.

const result = switche<number,string>(value)
    .case<number|string>(1, 'One')
    .case(2, 2) // Type error: Argument of type 'number' is not assignable to parameter of type 'string'.
    .default('Neither');