✨ 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
+2
-23
@@ -2,6 +2,8 @@ import type { Exact } from "type-fest";
|
||||
|
||||
import type {
|
||||
HandlerMap,
|
||||
PatternKey,
|
||||
PatternParam,
|
||||
PatternReturns,
|
||||
RedundantFallback,
|
||||
UnaryFn,
|
||||
@@ -11,29 +13,6 @@ import type {
|
||||
// the pair `true | false`; see README § Caveats for the unsupported members.
|
||||
type Matchable = string | number | boolean | null | undefined;
|
||||
|
||||
// `boolean`, `null` and `undefined` cannot be property keys, so a mapped type
|
||||
// over the universe keys each non-key member by its stringification. `Param`
|
||||
// inverts that projection, so a handler callback still receives the *real*
|
||||
// member (`true`, not `"true"`) — see README § Caveats for the limits.
|
||||
type PatternKey<T> = T extends boolean
|
||||
? T extends true
|
||||
? "true"
|
||||
: "false"
|
||||
: T extends null
|
||||
? "null"
|
||||
: T extends undefined
|
||||
? "undefined"
|
||||
: T;
|
||||
type PatternParam<K> = K extends "true"
|
||||
? true
|
||||
: K extends "false"
|
||||
? false
|
||||
: K extends "null"
|
||||
? null
|
||||
: K extends "undefined"
|
||||
? undefined
|
||||
: K;
|
||||
|
||||
type Handlers<T extends Matchable, R> = {
|
||||
[K in PatternKey<T>]: UnaryFn<PatternParam<K>, R>;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user