mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 12:32:08 -06:00
Initial investigation of exhaustiveness checking issue
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
parent
a0f8672288
commit
425a2c086d
49
test-issue.ts
Normal file
49
test-issue.ts
Normal file
@ -0,0 +1,49 @@
|
||||
// Test the issue: Exhaustiveness checking against an enum with 1 member
|
||||
|
||||
enum ActionTypes {
|
||||
INCREMENT = 'INCREMENT',
|
||||
}
|
||||
|
||||
interface IIncrement {
|
||||
payload: {};
|
||||
type: ActionTypes.INCREMENT;
|
||||
}
|
||||
|
||||
type AnyStringExcept<T extends string> = { [P in T]: never; };
|
||||
|
||||
type ValidAction = IIncrement;
|
||||
type UnhandledAction = { type: AnyStringExcept<ActionTypes>; };
|
||||
type PossibleAction = ValidAction | UnhandledAction;
|
||||
|
||||
function isUnhandled(x: PossibleAction): x is UnhandledAction {
|
||||
return !(x.type in ActionTypes);
|
||||
}
|
||||
|
||||
type CounterState = number;
|
||||
const initialState: CounterState = 0;
|
||||
|
||||
function receiveAction(state = initialState, action: PossibleAction) {
|
||||
if (isUnhandled(action)) {
|
||||
return state;
|
||||
}
|
||||
|
||||
// typeof action === ValidAction
|
||||
switch (action.type) {
|
||||
case ActionTypes.INCREMENT:
|
||||
return state + 1;
|
||||
}
|
||||
|
||||
// This should not error - all cases are handled
|
||||
const n: never = action;
|
||||
return state;
|
||||
}
|
||||
|
||||
// Simpler test case from RyanCavanaugh's comment
|
||||
function fn(obj: { name: "bob" }) {
|
||||
if (obj.name == "bob") {
|
||||
// bob case
|
||||
} else {
|
||||
// Should not be an error
|
||||
const n: never = obj;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user