File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments