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
Type inference fails to notice when a possibility has been eliminated by prior code.
In the code example, there are only four possibilities: both x & y are true, only x is true, only y is true, neither x nor y are true.
The first if/return eliminates the last possibility.
The second if/return eliminates the first and second possibilities, leaving only the third, that y is true, but code that expects y to be truthy after this point (e.g. when y is an object and you try to dereference a property) complains that y might be falsey (i.e. undefined/null) which we know it cannot be.
Took me a second to realize that famous quote was what you were referencing with “Arthur Conan Doyle was right”; that was clever. But yeah, the TS compiler is not actually Sherlock Holmes: #52822 (comment)
Bug Report
Type inference fails to notice when a possibility has been eliminated by prior code.
In the code example, there are only four possibilities: both x & y are true, only x is true, only y is true, neither x nor y are true.
The first if/return eliminates the last possibility.
The second if/return eliminates the first and second possibilities, leaving only the third, that y is true, but code that expects y to be truthy after this point (e.g. when y is an object and you try to dereference a property) complains that y might be falsey (i.e. undefined/null) which we know it cannot be.
🔎 Search Terms
🤷🏻♂️
🕗 Version & Regression Information
$ npx tsc --version
Version 4.9.5
⏯ Playground Link
Playground link with relevant code
💻 Code
bug also present when logic flipped:
🙁 Actual behavior
When hovering over it with the mouse cursor in VSCode, the type of
y
on third line of yIsTrue() resolves toboolean
i.e.true | false
.🙂 Expected behavior
The type of
y
on third line of yIsTrue() should resolve totrue
here, and vice versa for yIsFalse()“When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.”
The text was updated successfully, but these errors were encountered: