Skip to content

Commit 425a2c0

Browse files
Initial investigation of exhaustiveness checking issue
Co-authored-by: RyanCavanaugh <[email protected]>
1 parent a0f8672 commit 425a2c0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test-issue.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Test the issue: Exhaustiveness checking against an enum with 1 member
2+
3+
enum ActionTypes {
4+
INCREMENT = 'INCREMENT',
5+
}
6+
7+
interface IIncrement {
8+
payload: {};
9+
type: ActionTypes.INCREMENT;
10+
}
11+
12+
type AnyStringExcept<T extends string> = { [P in T]: never; };
13+
14+
type ValidAction = IIncrement;
15+
type UnhandledAction = { type: AnyStringExcept<ActionTypes>; };
16+
type PossibleAction = ValidAction | UnhandledAction;
17+
18+
function isUnhandled(x: PossibleAction): x is UnhandledAction {
19+
return !(x.type in ActionTypes);
20+
}
21+
22+
type CounterState = number;
23+
const initialState: CounterState = 0;
24+
25+
function receiveAction(state = initialState, action: PossibleAction) {
26+
if (isUnhandled(action)) {
27+
return state;
28+
}
29+
30+
// typeof action === ValidAction
31+
switch (action.type) {
32+
case ActionTypes.INCREMENT:
33+
return state + 1;
34+
}
35+
36+
// This should not error - all cases are handled
37+
const n: never = action;
38+
return state;
39+
}
40+
41+
// Simpler test case from RyanCavanaugh's comment
42+
function fn(obj: { name: "bob" }) {
43+
if (obj.name == "bob") {
44+
// bob case
45+
} else {
46+
// Should not be an error
47+
const n: never = obj;
48+
}
49+
}

0 commit comments

Comments
 (0)