✅ Check matcher handler params
The primitive union matchers invoke every handler with the matched literal, but the specs wrote their handlers with zero parameters, so the passed value and its inferred key-literal type went untested. Declare the parameter on each such handler and pin it with expectTypeOf, matching the style the two already-correct handlers used. The `_` fallbacks assert the whole union rather than a single literal. Return-type expectations are unchanged, so the W / non-W widening distinctions are still covered.
This commit is contained in:
1 parent
cce030b8e5
commit
ed71929365
1 file changed
+53
-13
+53
-13
@@ -21,8 +21,14 @@ test("getPrimitiveUnionMatcherW requires every literal key", () => {
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
const matcher = factory({
|
const matcher = factory({
|
||||||
a: () => 1 as const,
|
a: (s) => {
|
||||||
b: () => "two" as const,
|
expectTypeOf(s).toEqualTypeOf<"a">();
|
||||||
|
return 1 as const;
|
||||||
|
},
|
||||||
|
b: (s) => {
|
||||||
|
expectTypeOf(s).toEqualTypeOf<"b">();
|
||||||
|
return "two" as const;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -42,8 +48,14 @@ test("getPrimitiveUnionMatcherW dispatches on numeric literal keys", () => {
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
const matcher = factory({
|
const matcher = factory({
|
||||||
1: (n) => n + 1,
|
1: (n) => {
|
||||||
2: (n) => n * 10,
|
expectTypeOf(n).toEqualTypeOf<1>();
|
||||||
|
return n + 1;
|
||||||
|
},
|
||||||
|
2: (n) => {
|
||||||
|
expectTypeOf(n).toEqualTypeOf<2>();
|
||||||
|
return n * 10;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -62,8 +74,14 @@ test("getPrimitiveUnionMatcher infers a single return type shared by all handler
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
const matcher = factory({
|
const matcher = factory({
|
||||||
a: (): number => 1,
|
a: (s): number => {
|
||||||
b: (): 1 | 2 => 2,
|
expectTypeOf(s).toEqualTypeOf<"a">();
|
||||||
|
return 1;
|
||||||
|
},
|
||||||
|
b: (s): 1 | 2 => {
|
||||||
|
expectTypeOf(s).toEqualTypeOf<"b">();
|
||||||
|
return 2;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -107,7 +125,10 @@ test("getPrimitiveUnionMatcherPartial routes shapes without a handler to _", ()
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
const matcher = factory({
|
const matcher = factory({
|
||||||
a: (): 1 | 2 => 1,
|
a: (s): 1 | 2 => {
|
||||||
|
expectTypeOf(s).toEqualTypeOf<"a">();
|
||||||
|
return 1;
|
||||||
|
},
|
||||||
_: (s): 1 | 2 => {
|
_: (s): 1 | 2 => {
|
||||||
// The fallback sees the whole union, not a single literal.
|
// The fallback sees the whole union, not a single literal.
|
||||||
expectTypeOf(s).toEqualTypeOf<"a" | "b" | "c">();
|
expectTypeOf(s).toEqualTypeOf<"a" | "b" | "c">();
|
||||||
@@ -132,8 +153,14 @@ test("getPrimitiveUnionMatcherPartial also accepts an exhaustive pattern", () =>
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
const matcher = factory({
|
const matcher = factory({
|
||||||
a: () => "A",
|
a: (s) => {
|
||||||
b: () => "B",
|
expectTypeOf(s).toEqualTypeOf<"a">();
|
||||||
|
return "A";
|
||||||
|
},
|
||||||
|
b: (s) => {
|
||||||
|
expectTypeOf(s).toEqualTypeOf<"b">();
|
||||||
|
return "B";
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -154,8 +181,15 @@ test("getPrimitiveUnionMatcherPartialW allows gaps and widens to the union of ha
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
const matcher = factory({
|
const matcher = factory({
|
||||||
x: () => 1 as const,
|
x: (s) => {
|
||||||
_: () => "fallback" as const,
|
expectTypeOf(s).toEqualTypeOf<"x">();
|
||||||
|
return 1 as const;
|
||||||
|
},
|
||||||
|
_: (s) => {
|
||||||
|
// The fallback sees the whole union, not a single literal.
|
||||||
|
expectTypeOf(s).toEqualTypeOf<"x" | "y" | "z">();
|
||||||
|
return "fallback" as const;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -177,8 +211,14 @@ test("getPrimitiveUnionMatcherPartialW also accepts an exhaustive pattern", () =
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
const matcher = factory({
|
const matcher = factory({
|
||||||
x: () => 1 as const,
|
x: (s) => {
|
||||||
y: () => "two" as const,
|
expectTypeOf(s).toEqualTypeOf<"x">();
|
||||||
|
return 1 as const;
|
||||||
|
},
|
||||||
|
y: (s) => {
|
||||||
|
expectTypeOf(s).toEqualTypeOf<"y">();
|
||||||
|
return "two" as const;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
|||||||
Reference in new issue
Block a user