Skip to content

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

Closed
cevek opened this issue Sep 27, 2016 · 17 comments
Closed

true == false - cannot be applied to types 'false' and 'true'. #11178

cevek opened this issue Sep 27, 2016 · 17 comments

Comments

@cevek
Copy link

cevek commented Sep 27, 2016

TypeScript Version: Version 2.1.0-dev.20160927

Code

const a = false;
const b = a === true; //  Operator '===' cannot be applied to types 'false' and 'true'.
const c = true === false; // that is ok
@kitsonk
Copy link
Contributor

kitsonk commented Sep 27, 2016

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 boolean to a boolean instead of two incomparable literal types. The behaviour could be stricter to interpret literal values as literal types for comparison, but I can't see where it is being already more strict is a problem.

const a: boolean = false;
const b = a === true; // no issue

@cevek
Copy link
Author

cevek commented Sep 27, 2016

That issue from source code of mobx, it is simplified example
https://github.com./mobxjs/mobx/blob/master/src/api/autorun.ts#L225

@cevek
Copy link
Author

cevek commented Sep 27, 2016

Also https://github.com./mobxjs/mobx/blob/master/src/core/derivation.ts#L75
Operator '===' cannot be applied to types 'IDerivationState.POSSIBLY_STALE' and 'IDerivationState.STALE'.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 27, 2016

@cevek, that is different than the reported issue... there the comparison is between a let, which should not automatically infer a literal type of false. For example, this should work:

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
Copy link
Author

cevek commented Sep 27, 2016

ahejlsberg
This is working as intended. The compiler performs control flow analysis and knows that p has the actual value Place.Left where you're performing the === operations, and it is calling out that it makes no sense to compare a value that is known to be Place.Left to a value that is known to be Place.Right. It's effectively like writing Place.Left === Place.Right or 1 === 2, both of which would also produce errors.

@cevek cevek closed this as completed Sep 27, 2016
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Sep 27, 2016

@cevek Is this a bug in the mobx code, or did derivation.dependenciesState actually change value between lines 63 and 75?

@cevek
Copy link
Author

cevek commented Sep 27, 2016

Yeah, obj.get(); can change derivation.dependenciesState

@cevek
Copy link
Author

cevek commented Sep 27, 2016

duplicates of #10120 #11134

@jasonswearingen
Copy link

jasonswearingen commented Oct 10, 2016

@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 tsc compile error on the 3rd to last line (if (didAFix === true)):

src/validation.ts(120,7): error TS2365: Operator '===' cannot be applied to types 'false' and 'true'.

@RyanCavanaugh
Copy link
Member

@jasonswearingen see #9998 and the many bugs linked from it

@ghost
Copy link

ghost commented Jun 19, 2017

With typescript do this is work for me

let whatToDo: any =  false;

if (whatToDo === true) {
        this.pages.push(this.pageRange);
      }

@gsdnano
Copy link

gsdnano commented Jul 3, 2017

Same error when using simple statements such as

var b:boolean = false;
...
b === true ? 'n_' : ''

@kitsonk
Copy link
Contributor

kitsonk commented Jul 3, 2017

Code flow analysis is doing a better tracking of code than you might assume... In this case CFA has determined the only value b might have at this point in the code is false.

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 false so it is invalid to compare it to true.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jul 3, 2017

Unless ... changes the value of b such that it may become true, the compiler is correctly complaining about nonsense.

@se-schwarz
Copy link

se-schwarz commented Dec 7, 2017

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();
}

@RyanCavanaugh
Copy link
Member

@se-schwarz see #9998

@marquizzo
Copy link

marquizzo commented Jan 9, 2018

It feels like this would be less confusing if the error were worded better. My example is:

this.changed = true;
this.checkChanges();    // Changes the value of this.changed
if(this.changed === false){return;} // typescript operator === cannot be applied to true and false

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 true or false.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants