-
Notifications
You must be signed in to change notification settings - Fork 12.8k
true == false - cannot be applied to types 'false' and 'true'. #11178
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
Is there a real issue with this? While the error message is potentially confusing, it is valid and TypeScript is identifying a position where you have a logic error, although the third statement also has the same logic error it is because it is comparing a const a: boolean = false;
const b = a === true; // no issue |
That issue from source code of mobx, it is simplified example |
Also https://github.com./mobxjs/mobx/blob/master/src/core/derivation.ts#L75 |
@cevek, that is different than the reported issue... there the comparison is between a let a = false;
const b = a === true; // Operator '===' cannot be applied to types 'false' and 'true'. But the following works: let a = false;
a = true;
const b = a === true; // No error Which means the CFA is tracking the assigned literal type of the code. So I think the example need to be expanded upon, because the error isn't in the example, it is in CFA not properly tracking the type on assignment of a variable. |
|
@cevek Is this a bug in the mobx code, or did |
Yeah, |
@cevek: Please reopen this issue. control flow analysis fails when using closures such as this example: var didAFix = false;
_.forEach(this.invalid, (value:T, key:string) => {
var fixedValue = func(value, key, this);
if (fixedValue !== undefined) {
this._accept(key, fixedValue);
didAFix = true;
}
});
//fixup state after modifications
if (didAFix === true) { //tsc error TS2365
this._maintainValidState();
} I get a
|
@jasonswearingen see #9998 and the many bugs linked from it |
With typescript do this is work for melet whatToDo: any = false;
if (whatToDo === true) {
this.pages.push(this.pageRange);
} |
Same error when using simple statements such as
|
Code flow analysis is doing a better tracking of code than you might assume... In this case CFA has determined the only value The problem goes away, when the value cannot be statically determined anymore: var b: boolean = false;
b = Math.random() % 2 ? true : false;
b === true ? 'n_' : ''; It is the goal of TypeScript to statically identify constructs that are likely to be errors, which a variable, without reassignment, within the same closure cannot be any other value than |
Unless |
The following peace of code produces the error, while working perfectly fine. This comment from atlabiz works, but it's just a workaround. let somethingHappenedIndicator = false;
const listener = service.register(() => {
somethingHappenedIndicator = true;
});
anotherService.doSomethingThatMayTriggerTheRegisteredCallback();
listener.unsubscribe();
if (somethingHappenedIndicator === true) { // TS2365:Operator '===' cannot be applied to types 'false' and 'true'
doSomething();
} |
@se-schwarz see #9998 |
It feels like this would be less confusing if the error were worded better. My example is:
If it said "variable assigned value of true; it is redundant to compare to false" it would've become immediately clear what was wrong. Instead, it led me to believe that I was taking crazy pills and a strict equality could not be compared to |
TypeScript Version: Version 2.1.0-dev.20160927
Code
The text was updated successfully, but these errors were encountered: