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
{{ message }}
Repository navigation
Narrowing type via Function Assertion loses numeric type #37807
// GOOD EXAMPLEfunctionf1(x: any): asserts x is number|undefined{}letv1: number|string|undefined;f1(v1)typet1=typeofv1;// t1 = number | undefined, as excepted// BUGGY EXAMPLEfunctionf2(x: any): asserts x is 6|undefined{}letv2: number|string|undefined;f2(v2)typet2=typeofv2;// t2 = undefined. Expected to be 6 | undefined
Related Issues: I went through the comments of the popular #32695 by Anders Hejlsberg (@ahejlsberg) and also searched in the existing issues but couldn't find any similar problem being reported in the past.
FWIW, I ran into this trying to write a assertIsInEnumValues for Numeric Enums. (related StackOverflow question: https://stackoverflow.com/a/61058457)
Jack Williams (@jack-williams) thanks for commenting. It's definitely not limited to undefined (I just tested and it has the same bug with boolean as well). However, I can definitely smell that "branded primitives" discussed at #31206 might have caused it.
Activity
jack-williams commentedon Apr 6, 2020
I think this is a duplicate of #31156
eyedean commentedon Apr 6, 2020
Jack Williams (@jack-williams) thanks for commenting. It's definitely not limited to
undefined(I just tested and it has the same bug withbooleanas well). However, I can definitely smell that "branded primitives" discussed at #31206 might have caused it.sandersn commentedon Apr 7, 2020
Yep, it's a duplicate of #31156 -- the unions on the source and target are the cause. Swapping
undefinedforbooleanhas the same problem.