nanocase
v0.1.0
Published
Tiny exhaustive matching for TypeScript discriminated unions
Maintainers
Readme
nanocase
Small, exhaustive matching for TypeScript discriminated unions.
import { match } from "nanocase";
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number };
const area = (shape: Shape) =>
match(shape).on("kind", {
circle: ({ radius }) => Math.PI * radius ** 2,
square: ({ size }) => size ** 2,
});Missing cases are TypeScript errors.
Patterns
import { match } from "nanocase/pattern";
match(shape, (m) =>
m.with({ kind: "circle" }, () => "round").otherwise(() => "other"),
);