0.1.0 • Published 2d ago
nanocase
Licence
MIT
Version
0.1.0
Deps
0
Size
9 kB
Vulns
0
Weekly
0
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"),
);