✨ Allow boolean and nullish tags in tagged unions
A `boolean` / `null` / `undefined` discriminant cannot key a handler map, so the tagged-union matcher now routes it through the `PatternKey` / `PatternParam` projection the primitive-union matcher already used. Both matchers share the projection from `matcher-shared.ts`; `MapTaggedUnion`, `Handlers`, `HandledMembers` and `MustBePartial` key off the projected form and invert it to recover the real member. Covers exhaustive and fallback dispatch, the redundant-fallback guard, and the LSP popup offering the tags by name.
This commit is contained in:
1 parent
b81a8149d9
commit
e96ca12149
5 files changed
+243
-52
No files matched your search
@@ -8,6 +8,32 @@ import type { ValueOf } from "type-fest";
|
||||
// A handler: one universe member in, one return value out.
|
||||
export type UnaryFn<T, R> = (shape: T) => R;
|
||||
|
||||
// `boolean`, `null` and `undefined` cannot be property keys, so a mapped type
|
||||
// over a universe that includes one keys each such member by its
|
||||
// stringification. `PatternParam` inverts that projection, so a handler callback
|
||||
// still receives the *real* member (`true`, not `"true"`). Both matchers use the
|
||||
// projection: the primitive-union matcher over its universe, the tagged-union
|
||||
// matcher over a discriminant property's values. See README § Caveats for the
|
||||
// limits.
|
||||
export type PatternKey<T> = T extends boolean
|
||||
? T extends true
|
||||
? "true"
|
||||
: "false"
|
||||
: T extends null
|
||||
? "null"
|
||||
: T extends undefined
|
||||
? "undefined"
|
||||
: T;
|
||||
export type PatternParam<K> = K extends "true"
|
||||
? true
|
||||
: K extends "false"
|
||||
? false
|
||||
: K extends "null"
|
||||
? null
|
||||
: K extends "undefined"
|
||||
? undefined
|
||||
: K;
|
||||
|
||||
// `Extract` drops optional handlers (`undefined`) so `PatternReturns` also works
|
||||
// when `P`'s constraint has optional keys.
|
||||
export type PatternReturns<P> = ReturnType<
|
||||
|
||||
Reference in new issue
Block a user