-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: 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
MaceWindu and MichalLytek
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: 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