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
The example is kind of contrived, but it's a very simplified version of something from a real-world codebase. I jumped through hoops to get it to clearly show an error in the failing case.
🙁 Actual behavior
When the "job" function has the type of logger inferred as below, reachability/control flow analysis incorrectly determines that val could be of type null on the console.log(...) line:
executeJob((val,logger)=>{if(!Array.isArray(val)){logger.exception('testing');}console.log(val.indexOf('testing'));// 'val' is possibly 'null'. (18047)});
However, when the type of Logger is explicitly annotated, things work as expected and the type of val is correctly narrowed to string[] after logger.exception(...) is called.
executeJob((val,logger: Logger)=>{if(!Array.isArray(val)){logger.exception('testing');}console.log(val.indexOf('testing'));// no error, as expected});
🙂 Expected behavior
I would expect the first example above with an implicit type for logger to behave the same as when the type of logger is explicitly declared to be Logger.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
"return never type inference", "return never control flow"
I came across #12825, but the code snippet there works as expected, and I have something similar working as expected in my reproduction.
I also saw #34955, but it's not clear to me if it's related to my issue or not (it seems separate).
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgDIHsDmnrIN4BQyyEAHkgA5jDogAUAtgM6YBcyTYUomAlOyAgA3aAG4CAXwIEYAVxAJqtEqQgJZkAFLoARnQBWu9nSFwANu07cQmANoBdZAB9kIWWbMAaZGaw4o7BjY0LzIALwAfMhC6MAAJqGExAi0nD5+0IEZUOH4RMQqlEr0zGwcXDyJ+QXIYAAWUOgA7q4QLQCiUI1QjCy84jUSnvkS4vmGem4e3r7BUP2S0mRqGhDaeibmM9mhkXnEwDDIdACEAIJdcACeAHTATBdQ15tmvFU1s-43ZEU09ADkkE4PH+C2IUnyKRATHQZggN1mLzuIDiZAA8jA6ICIMCbKCFhIFgRluotLo6C9tnMsnNdlEkshDsdzpdbvdHs9TK93gVPtBvuQIFQ-ligdQ8WDkBDkqlYfDEVzkajSBjRTjxZh8eJCeIgA
💻 Code
The example is kind of contrived, but it's a very simplified version of something from a real-world codebase. I jumped through hoops to get it to clearly show an error in the failing case.
🙁 Actual behavior
When the "job" function has the type of
logger
inferred as below, reachability/control flow analysis incorrectly determines thatval
could be of typenull
on theconsole.log(...)
line:However, when the type of
Logger
is explicitly annotated, things work as expected and the type ofval
is correctly narrowed tostring[]
afterlogger.exception(...)
is called.🙂 Expected behavior
I would expect the first example above with an implicit type for
logger
to behave the same as when the type oflogger
is explicitly declared to beLogger
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: