TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

TS does NOT understand the condition will always be true

Open aghArdeshir opened this issue 3 years ago • 1 comments

Bug Report (I'm not sure, maybe feature request)

🔎 Search Terms

It was hard to search for this. I tried "this condition will always return true" and "any condition always true" both in google and TypeScript GitHub repository issues. I found nothing.

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "condition"s.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.9.0-dev.20220921#code/MYewdgzgLgBAHgLhmArgWwEYFMBOMC8MAsgIZQAWAdDiWACYhoAUAlANwBQAlgGYxNwYAQnyEAjDAA+k+MNEwATCxgBvGAHp1MAKI4cIHEhT1c0WnQgwKWGKHpcoXcDADuXADbuYJdy5IBPS2wrHBQsShgAcRwsMhhAUHIOGFtwCBB3cPcQAHMBFg4AXw4OO2gYfyRafwJiMioaekZWTl5+apFxKRl2+SVVDS0AERAsSzAQWGM6UyhzK3IuSzs6BycwVw8vHz9AmGCoUJtAGXIklMh0zJy8wo4gA

💻 Code

const x: number = Math.random();
if (x !== 1 || x !== 2) { // Error: understands the condition will always be true. Great ✅
  console.log(x)
}

const y: any = Math.random();
if (y !== 1 || y !== 2) { // Does not understand this condition will always be true ❌
  console.log(x)
}

🙁 Actual behavior

I was writing the wrong code. I used an || OR condition instead of a && AND condition. TypeScript caught my bug right away. I was so happy. But I noticed in another part of the code I made the same mistake, but TS did not complain about it. I checked a little more and found out it was because the other variable was of any type.

🙂 Expected behavior

I expect TypeScript understand this problem regardless of the variable type. If it is the same variable, it can't be both values simultaneously. MyVar !== x && MyVar !== y is always true, regardless of types of them; because MyVar only has one value at a time.

aghArdeshir avatar Sep 22 '22 13:09 aghArdeshir

One of the design principles of any is that, to the greatest extent possible, it should be impossible to get a type error in an expression involving any

RyanCavanaugh avatar Sep 22 '22 20:09 RyanCavanaugh

Thank you @RyanCavanaugh. To my eyes, it seemed like a logical-related problem rather than a type-related problem. I, personally, sometimes do use any as an escape-hatch for complicated "type" situations. But I believe this case is different, it is not a complex "type" situation, it is a "logical" error, and an opportunity to remove dead code. A piece of code that never executes (and most probably will be removed in later build stages via babel etc...). The escape-hatch for these scenarios, just to keep TS silent would be @ts-ignore for me.

Maybe my requirement is something that needs to be done by a linter rather than TypeScript. Maybe eslint is where I should look into.

This comment is a no-op. I just mentioned how it seemed to me. You can close this issue if that's a design decision.

Thank you for your time.

aghArdeshir avatar Sep 23 '22 06:09 aghArdeshir