Repository navigation
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
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
Reacted by MaceWindu and Michał Lytek
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisFixedA PR has been merged for this issueA PR has been merged for this issue
Activity
noahm commentedon Dec 19, 2018
Here's another example of a regression in type narrowing behavior from 3.0 that seems related. It seems like the
item.user.loginis being narrowed correctly in the example but the former is not. If you swap the order of the conditionals in the if statement then the error moves fromidto theloginproperty.Playground link (turn on strictNullChecks)