Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/orouz/tiny-ts-matcher

type safe replacement for switch statements
https://github.com/orouz/tiny-ts-matcher

pattern-matching typescript

Last synced: 1 day ago
JSON representation

type safe replacement for switch statements

Awesome Lists containing this project

README

        



tiny-ts-matcher






License: MIT


type safe replacement for switch statements


## **Install**

`npm i tiny-ts-matcher`


## **Usage**

```typescript
import { createMatcher } from "tiny-ts-matcher";

const match = createMatcher("status");

type ServerResponse =
| { status: 500; message: string }
| { status: 400; error: string };

const result = match()({
500: ({ message }) => message,
400: ({ error }) => error,
_: () => "no match",
})({ status: 500, message: "woops" });

// result: woops
```