@hecto/pattern
v3.0.0
Published
Exhaustive, structural pattern matching for TypeScript
Readme
@hecto/pattern
Exhaustive, structural pattern matching for TypeScript. Patterns are plain values plus tiny predicate objects. There is no service container, effect runtime, code generation step, or global registry.
pnpm add @hecto/patternExhaustive matching
import { match, select, string } from '@hecto/pattern'
type Event =
| { type: 'created'; id: number }
| { type: 'renamed'; id: number; name: string }
| { type: 'deleted'; id: number }
const label = (event: Event) =>
match(event)
.with({ type: 'created' }, ({ id }) => `created ${id}`)
.with(
{ type: 'renamed', name: select('name', string) },
({ id }, { name }) => `renamed ${id} to ${name}`,
)
.with({ type: 'deleted' }, ({ id }) => `deleted ${id}`)
.exhaustive()exhaustive() is only callable after every member of a discriminated union is
covered. Use otherwise(handler) for an explicit fallback or run() when an
unmatched result should be undefined.
For dispatch tables, discriminant(key, value, handlers), tag(value,
handlers), and value(value, handlers) require a complete handler map.
Patterns
Literal values and nested object/tuple patterns work directly. Reusable combinators include:
- primitive matchers:
string,number,finite,bigint,boolean,symbol,object,defined, andnullish; literal,union,intersection, andnot;when,instanceOf,property,optional, andnullable;array,tuple, partialrecord, exact-keystrict,setOf, andmapOf;select(name, pattern)for typed named captures;test,isMatching,extract, andassertfor guard-style use.
Subpath imports are available from @hecto/pattern/match and
@hecto/pattern/pattern.
