Skip to content

Commit d210075

Browse files
committed
fix: repair typescript not complaining of missing keys
1 parent f9e9331 commit d210075

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

.changeset/thin-panthers-know.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"exhaustive": patch
3+
---
4+
5+
Fix exhaustive compile checks
6+
7+
With the added support of exhaustive boolean checks, TypeScript stopped complaining if a key was missing in the exhaustive object due to how the new types were declared. The types were adjusted to bring back the expected behavior of TypesScript complaining at compile-time if you forget to handle all the use-cases.

src/exhaustive.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@ import { corrupt } from './corrupt';
22

33
type AnyFunction = (...args: any[]) => unknown;
44

5-
export type ExhaustiveUnion<Union extends string | boolean> =
6-
Union extends string
7-
? {
8-
[Key in Union]: (value: Key) => any;
9-
} & ExhaustiveFallback
10-
: Union extends boolean
11-
? {
12-
[Key in `${Union}`]: (value: Key extends 'true' ? true : false) => any;
13-
} & ExhaustiveFallback
14-
: never;
5+
export type ExhaustiveUnion<Union extends string | boolean> = {
6+
[Key in `${Union}`]: (
7+
value: Key extends 'true' ? true : Key extends 'false' ? false : Key,
8+
) => any;
9+
} & ExhaustiveFallback;
1510

1611
export type ExhaustiveTag<
1712
Union extends object,
1813
Tag extends keyof Union,
19-
Values extends Union[Tag] = Union[Tag],
20-
> = Values extends string
14+
> = Union[Tag] extends string | boolean
2115
? {
22-
[Key in Values]: (value: Extract<Union, { [K in Tag]: Key }>) => any;
23-
} & ExhaustiveFallback
24-
: Values extends boolean
25-
? {
26-
[Key in `${Values}`]: (
16+
[Key in `${Union[Tag]}`]: (
2717
value: Extract<
2818
Union,
29-
{ [K in Tag]: Key extends 'true' ? true : false }
19+
{
20+
[K in Tag]: Key extends 'true'
21+
? true
22+
: Key extends 'false'
23+
? false
24+
: Key;
25+
}
3026
>,
3127
) => any;
3228
} & ExhaustiveFallback
@@ -80,7 +76,6 @@ function exhaustive(
8076
const unionObject = unionOrObject as object;
8177
const keyofUnion = matchOrKeyofUnion as keyof typeof unionObject;
8278

83-
// @ts-expect-error super generic overload implementation will fallback to never
8479
return exhaustive.tag(unionObject, keyofUnion, match);
8580
}
8681

0 commit comments

Comments
 (0)