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 🙏

© 2025 – Pkg Stats / Ryan Hefner

typed-match

v0.0.1

Published

typescript pattern matching

Downloads

4

Readme

typescript 模式匹配

模仿其他语言的模式匹配而在 typescript 上的不完整实现

使用

import { match, Type } from '@cj97/match';

type Foo = Type<'Foo', {
    foo: string
}>;

type Bar = Type<'Bar', {
    bar: number
}>;

function test (x: Foo | Bar | undefined | boolean) {
    // const result: string | number | boolean | undefined
    // 自动推断 result 类型
    const result = match(x)({
        // 此处会提示补全所有匹配
        Foo: foo => foo.foo,
        Bar: bar => bar.bar,
        undefined: () => undefined,
        boolean: v => !v
    });
    return result;
}
test({ __type: 'Foo', foo: 'hello' }); // hello

也可以使用下面这种方式使用类的声明

class Foo extends TypeClass<'Foo'> {
    __type: 'Foo' = 'Foo'
    foo: string
    constructor ({ foo }: { foo: string }) {
        super();
        this.foo = foo;
    }
}

对于值匹配,需要使用另外的函数

const tests = Symbol('test');
function test (x: 123 | typeof tests | undefined) {
    const result = matchValue(x)({
        [tests]: (v) => 'symbol',
        123: (v) => '123',
        DEFAULT: (v) => { throw new Error('unexpected value'); }
    });
}

需要明确参数可能的取值,以下形式会引起错误

const tests = Symbol('test');
function test (x: number | string | typeof tests | undefined) {
    const result = matchValue(x)({
        [tests]: (v) => 'symbol',
        123: (v) => '123',
        DEFAULT: (v) => 'other'
    });
}

但是目前支持

const tests = Symbol('test');
function test (x: number | typeof tests | undefined) {
    const result = matchValue(x)({
        [tests]: (v) => 'symbol',
        123: (v) => '123',
        DEFAULT: (v) => 'other'
    });
}
// or
function test (x: string | typeof tests | undefined) {
    const result = matchValue(x)({
        [tests]: (v) => 'symbol',
        123: (v) => '123',
        DEFAULT: (v) => 'other'
    });
}

注意

必须在 tsconfig 设置 "strictNullChecks": true 否则无法进行正确的类型推断

match 参数不允许类型为 null 。考虑到 undefinednull 在开发期间对应的处理函数是一致的,而实现方式导致入参联合类型及匹配必须一一对应,同时 typeof undefined === 'undefined'typeof null === 'object',为了实现的简洁,所以选择只允许 undefined