You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// A self-contained demonstration of the problem follows...interfaceA{}interfaceB{}interfaceC{}declarefunctionisA(arg: any): arg is A;declarefunctionisB(arg: any): arg is B;declarefunctionisC(arg: any): arg is C;functionhandleAction<T>(action: T){switch(true){caseisA(action):
caseisB(action):
// action expected to be A|B type herebreak;caseisC(action):
// action expected to be C type herebreak;default:
// action expected to be undefined type here}}
Unfortunately, the switch code uses a form that is not recognised as a narrowing form. switch is really only used to narrow discriminated unions:
functionf(x: First|Second|Third){switch(x.kind){caseKind.First:
x;// x is now Firstbreak;// etc}}
Note that even if you rewrite to if, not all expressions are usable for narrowing. For example,
if(true===isA(action)){action;// action is not narrowed}if(isA(action)){action;// action is narrowed to A}
In order to keep things simple, narrowing only supports a few common syntactic forms. We do expand this list from time to time, but I don't think the switch (true) pattern is common enough to make it on the list.
Activity
sandersn commentedon Oct 13, 2016
Unfortunately, the
switchcode uses a form that is not recognised as a narrowing form.switchis really only used to narrow discriminated unions:Note that even if you rewrite to
if, not all expressions are usable for narrowing. For example,In order to keep things simple, narrowing only supports a few common syntactic forms. We do expand this list from time to time, but I don't think the
switch (true)pattern is common enough to make it on the list.RyanCavanaugh commentedon Nov 15, 2016
Not a common enough pattern to support