`getMatcher` / `getMatcherW` become `getPrimitiveUnionMatcher` / `getPrimitiveUnionMatcherW`; `src/primitive.ts` and its test move to `src/primitive-union.*`. The `Union` suffix mirrors `getTaggedUnionMatcher`.
29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
import type { ValueOf } from "type-fest";
|
|
|
|
// The primitive-union and tagged-union matchers differ in their universe, but the
|
|
// handler/fallback plumbing is identical; these are the shared pieces. The
|
|
// boundary is deliberate: `Handlers`, `Fallback` and `MustBePartial` stay with
|
|
// each matcher because they are built from its universe. See development/library.md.
|
|
|
|
// A handler: one universe member in, one return value out.
|
|
export type UnaryFn<T, R> = (shape: T) => R;
|
|
|
|
// `Extract` drops optional handlers (`undefined`) so `PatternReturns` also works
|
|
// when `P`'s constraint has optional keys.
|
|
export type PatternReturns<P> = ReturnType<
|
|
Extract<ValueOf<P>, (...args: never[]) => unknown>
|
|
>;
|
|
|
|
// The diagnostic raised when a fallback is supplied for an already-exhaustive
|
|
// handler map. Each matcher's `MustBePartial` folds it into `Handled`'s
|
|
// constraint so the guard is checked after inference.
|
|
export interface RedundantFallback {
|
|
readonly "every case is already handled, so the fallback is redundant": never;
|
|
}
|
|
|
|
// The runtime dispatch map the handler maps and fallback erase to.
|
|
export type HandlerMap = Record<
|
|
string | number,
|
|
UnaryFn<never, unknown> | undefined
|
|
>;
|