TypeScript Version: 3.9.0-dev.20200406
Search Terms: assertion, function, narrowing, numeric types
Code
// 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 | undefinedExpected behavior:t2 must be 6 | undefined.
Actual behavior:t2 is undefined only.
Playground Link:Playground Link
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)
TypeScript Version: 3.9.0-dev.20200406
Search Terms: assertion, function, narrowing, numeric types
Code
Expected behavior:
t2must be6 | undefined.Actual behavior:
t2isundefinedonly.Playground Link:Playground Link
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
assertIsInEnumValuesfor Numeric Enums. (related StackOverflow question: https://stackoverflow.com/a/61058457)