orouz / tiny-ts-matcher

type safe replacement for switch statements

Repository from Github https://github.comorouz/tiny-ts-matcherRepository from Github https://github.comorouz/tiny-ts-matcher

tiny-ts-matcher

License: MIT

type safe replacement for switch statements

Install

npm i tiny-ts-matcher


Usage

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

const match = createMatcher("status");

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

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

// result: woops

About

type safe replacement for switch statements


Languages

Language:TypeScript 97.1%Language:JavaScript 2.9%