Closed
Description
TypeScript Version: 3.2.2, 3.3.0-dev.201xxxxx
Search Terms: 3.2 switch union narrowing regression
Code
type Additive = '+' | '-';
type Multiplicative = '*' | '/';
interface AdditiveObj {
key: Additive
}
interface MultiplicativeObj {
key: Multiplicative
}
type Obj = AdditiveObj | MultiplicativeObj
export function foo(obj: Obj) {
switch (obj.key) {
case '+': {
onlyPlus(obj.key);
return;
}
}
}
function onlyPlus(arg: '+') {
return arg;
}
Expected behavior:
obj.key
is narrowed to '+'
in the switch and the call to onlyPlus(obj.key)
type checks successfully.
Actual behavior:
obj.key
is narrowed to Additive
in the switch and the call to onlyPlus(obj.key)
fails to type check
Related Issues:
- TS 3.2.0-rc seems to forget union type refinements more eagerly #28568
- Fix switch case control flow #27612
- Bring typeof switch closer inline with if #27680
Note:
This seems to be a regression between TS 3.1 and TS 3.2