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
interfaceA{type: 'A'}interfaceB{type: 'B'}functionisA(x: A|B): x is A{returnx.type==='A';}functionisB(x: A|B): x is B{returnx.type==='B';}// Using if/else with type guard functionsfunctionfoo1(x: A|B): any{x// x is A | Bif(isA(x)){returnx;// x is A}x// x is B |if(isB(x)){returnx;// x is B}x// x is B, but should be never <===== (1)}// Using if/else with discriminated unionsfunctionfoo2(x: A|B): any{x// x is A | Bif(x.type==='A'){returnx;// x is A}x// x is Bif(x.type==='B'){returnx;// x is B}x// x is never <===== (2)}
Expected behavior: x is narrowed to never at both (1) and (2)
Actual behavior: x is narrowed to never at (2), but at (1) it is narrowed to B.
yortus
changed the title
Different narrowing behaviour of type guards vs discriminated unions
Different narrowing behaviour of user-defined type guards vs discriminated unions
Aug 5, 2016
TypeScript Version: nightly (2.1.0-dev.20160804)
Code
Expected behavior:
x
is narrowed tonever
at both (1) and (2)Actual behavior:
x
is narrowed tonever
at (2), but at (1) it is narrowed toB
.Notes:
This is carried over from #9260 which is closed. See #9260 (comment)
The text was updated successfully, but these errors were encountered: