-
Notifications
You must be signed in to change notification settings - Fork 12.8k
"This condition will always return 'false'" but returns true at runtime #26592
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
Comments
Yeah, we discussed |
I think just using the old error message is probably fine. |
related #24991 |
I got this error EDIT : Sorry, Typescript current version on Stackblitz shouldn't be 3.1.x |
@HZooly That links to a document that doesn't use |
@Andy-MS Sorry my snippet wasn't saved! Now it has been updated :) |
Thank you for pointing me to this interesting discussion! |
I get this very annoying bug even with
|
@ConnorsFan See #9998 |
@Andy-MS The error goes away when I initialize the value as a number. I don't know if that is a recommended workaround:
|
May be better to write |
@Andy-MS I don't understand your last comment. Maybe I should add that the bug is also present if I do this:
In other words, I don't know how to make the code work when |
If you write |
OK, it works. Sorry, I had misread you previous comment. I didn't think that you meant |
I have a similar scenario. Here is the minimal reproduction: enum types {
type1 = 'type1',
type2 = 'type2'
}
class A {
private h: types = types.type1;
private a() {
this.h = types.type2;
this.b();
// Actual error here: the method b changes h to types.type1. But typescript thinks this.h would always be types.type2.
if (this.h === types.type1) { // This condition will always return 'false' since the types 'types.type2' and 'types.type1' have no overlap.
console.log('hello')
}
}
private b() {
this.h = types.type1;
}
} See it live here: https://stackblitz.com/edit/typescript-qvs8ov |
Is this not getting fixed? |
I started to use @typescript-eslint/strict-boolean-expressions after I found a silly mistake in my code that could have costed me a lot of money, as I did a This rule is very good, but, doesn't work very good with TS due to a current fact: If you want to check a falsy value (e.g. empty string and undefined), with I have made, before this So, Current TS version: 4.1.2 |
are we getting fix? |
Can someone point more specifically to where this error-check happens, to facilitate a PR that disables the check for == and != cases? I'm not able to find it, even in code search. |
It really annoys me that this has been a bug for years and looks like the devs here have no interest in resolving it. Not only is the error message misleading and wrong, TypeScript is failing to be a superset of JavaScript as it's not allowing me to do perfectly valid JavaScript. Frankly whether anyone considers it to be bad practice to allow an implicit conversion or not is irrelevant. We who are developing our own code should be allowed to make that choice for ourselves. I as the developer should be able to choose what parts of the language I use to the fullest. |
It has never been the case that no JS you could possibly write would issue zero errors in TypeScript, nor would this make any sense. See https://stackoverflow.com/questions/41750390/what-does-all-legal-javascript-is-legal-typescript-mean |
TypeScript Version: 3.1.0-dev.20180818
Code
Expected behavior:
Error message is 100% accurate.
Actual behavior:
The
==
operator in JavaScript converts its arguments -- some developers apparently rely on this behavior (not naming names but I noticed this in real JavaScript code that I pasted into a TypeScript file).The error message might recommend using an explicit conversion such as
Number(s)
to get this to compile in TypeScript.CC @DanielRosenwasser
The text was updated successfully, but these errors were encountered: