import type { Simplify, ValueOf } from "type-fest"; type UnaryFn = (shape: T) => R; // ============================================================================ // ✔️ Exhaustive // ❌ ReturnsStrict // ============================================================================ type PatternPrimitiveUnion = { [K in T]: UnaryFn; }; type PatternReturns< P extends Record>, > = ReturnType>; export const getPrimitiveUnionMatcherW: () => < P extends PatternPrimitiveUnion, >( pattern: Simplify

, ) => UnaryFn> = () => (pattern) => (shape) => // Rewrite not to use any is possible, was evaluated and solutions were // more complex than the current solution. // oxlint-disable-next-line typescript/no-explicit-any typescript/no-unsafe-type-assertion (pattern[shape] as any)(shape); // ============================================================================ // ✔️ Exhaustive // ✔️ ReturnsStrict // ============================================================================ export const getPrimitiveUnionMatcher: () => ( pattern: Simplify>, ) => UnaryFn = getPrimitiveUnionMatcherW; // ============================================================================ // ❌ Exhaustive // ✔️ ReturnsStrict // ============================================================================ type PatternPrimitiveUnionPartial = | PatternPrimitiveUnion | (Partial> & { _: UnaryFn; }); export const getPrimitiveUnionMatcherPartial: () => < R, >( pattern: Simplify>, ) => UnaryFn = () => (pattern) => (shape) => // Rewrite not to use any is possible, was evaluated and solutions were // more complex than the current solution. // oxlint-disable-next-line typescript/no-explicit-any typescript/no-unsafe-type-assertion (pattern[shape] ?? (pattern as any)["_"])(shape); // ============================================================================ // ❌ Exhaustive // ❌ ReturnsStrict // ============================================================================ export const getPrimitiveUnionMatcherPartialW: < T extends string | number, >() =>

>( // `Simplify

` is the inference hook: callers infer `P` from the argument. // the second half pins the impl parameter's `R` to `PatternReturns

`. // that makes the `= getPrimitiveUnionMatcherPartial` assignment type-check. // neither half works alone. // without the witness the union's `_` arm demands `_ ∈ keyof P`. // without `Simplify

` the parameter types do not compare. pattern: Simplify

& PatternPrimitiveUnionPartial, T>, ) => UnaryFn> = getPrimitiveUnionMatcherPartial;