Skip to content
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

Incorrect types within false condition #50194

Closed
cns-solutions-admin opened this issue Aug 5, 2022 · 2 comments
Closed

Incorrect types within false condition #50194

cns-solutions-admin opened this issue Aug 5, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@cns-solutions-admin
Copy link

cns-solutions-admin commented Aug 5, 2022

Bug Report

Typescript does not evaluate all conditions to determine actual types, if the condition is always false because of a literal "false".
Adding a "false &&" to a condition is often done during debugging.

🔎 Search Terms

false condition, type

🕗 Version & Regression Information

4.7.3, 4.7.4

⏯ Playground Link

none

💻 Code

function test(param?: string) {
  if (param) {
    return param.length;
  }
  return 0;
}

function brokenTest(param?: string) {
  if (false && param) {
    return param.length; // <= TS2532: Object is possibly 'undefined'.
  }
  return 0;
}

function workaroundTest(param?: string) {
  if (!true && param) {
    return param.length; // compiles
  }
  return 0;
}

🙁 Actual behavior

When adding a "false &&" to a condition of an if, the original condition is ignored for the type checks in the if block.

🙂 Expected behavior

It still compiles.

@MartinJohns
Copy link
Contributor

Duplicate of #26914.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 5, 2022
@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

4 participants