Skip to content

Type inference elimination bug shows Arthur Conan Doyle was right. #54388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nickshanks opened this issue May 25, 2023 · 3 comments
Closed

Type inference elimination bug shows Arthur Conan Doyle was right. #54388

nickshanks opened this issue May 25, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@nickshanks
Copy link

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

function yIsTrue(x: boolean, y: boolean) {
  if (!x && !y) return;
  if (x) return;
  y; // TS thinks y is `true | false` but it can only be `true` here.
}

bug also present when logic flipped:

function yIsFalse(x: boolean, y: boolean) {
  if (x && y) return;
  if (!x) return;
  y; // TS thinks y is `true | false` but it can only be `false` here.
}

🙁 Actual behavior

When hovering over it with the mouse cursor in VSCode, the type of y on third line of yIsTrue() resolves to boolean i.e. true | false.

🙂 Expected behavior

The type of y on third line of yIsTrue() should resolve to true here, and vice versa for yIsFalse()

“When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.”

@MartinJohns
Copy link
Contributor

Essentially a duplicate of #54252.

@fatcerberus
Copy link

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)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 26, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants