Open
Description
TypeScript Version: [email protected]
Search Terms: discriminated, exhaustiveness, type guard, narrowing
Code
// Legal action types for ValidAction
enum ActionTypes {
INCREMENT = 'INCREMENT',
// DECREMENT = 'DECREMENT',
}
interface IIncrement {
payload: {};
type: ActionTypes.INCREMENT;
}
// interface IDecrement {
// payload: {};
// type: ActionTypes.DECREMENT;
// }
// Any string not present in T
type AnyStringExcept<T extends string> = { [P in T]: never; };
// ValidAction is an interface with a type in ActionTypes
type ValidAction = IIncrement;
// type ValidAction = IIncrement | IDecrement;
// UnhandledAction in an interface with a type that is not within ActionTypes
type UnhandledAction = { type: AnyStringExcept<ActionTypes>; };
// The set of all actions
type PossibleAction = ValidAction | UnhandledAction;
// Discriminates to ValidAction
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) {
// typeof action === PossibleAction
if (isUnhandled(action)) {
// typeof action === UnhandledAction
return state;
}
// typeof action === ValidAction
switch (action.type) {
case ActionTypes.INCREMENT:
// typeof action === IIncrement
return state + 1;
// case ActionTypes.DECREMENT:
// return state - 1;
}
// typeof action === IIncrement
// Since INCREMENT is handled above, this should be impossible,
// However the compiler will say that assertNever cannot receive an argument of type IIncrement
return assertNever(action);
}
function assertNever(x: UnhandledAction): never {
throw new Error(`Unhandled action type: ${x.type}`);
}
Expected behavior: No error would be thrown, as the switch statement is exhaustive. If the ActionTypes.DECREMENT parts are uncommented (resulting in two possible values for ActionTypes) there is no error. An error only occurs when ActionTypes takes on a single value. The error occurs even if the never
assertion happens in the default statement, which is obviously unreachable from IIncrement.
Actual behavior: An error is thrown despite the only possible value being explicitly handled. If ActionTypes.DECREMENT is uncommented the expected behavior is present.