Check out this code:
export class Smth {
private flag: boolean;
public update() {
this.flag = true;
this.inner();
if (this.flag === false) { // Operator '===' cannot be applied to types 'true' and 'false'.
console.log(123);
}
}
private inner() {
this.flag = false;
}
}
I'm having trouble understanding the error in the line:
if (this.flag === false)
The TypeScript compiler is giving me an error message that says:
Operator '===' cannot be applied to types 'true' and 'false'.
But I am working with type boolean
and value false
.
I am currently using TypeScript version 2.6.2, but even on the online playground, I get the same result with version 2.7.
This issue is not the same as Operator '==' cannot be applied to types x and y in Typescript 2, as that problem deals with comparing constants. In my code, it involves a changeable class field and there is a function that modifies its value. Additionally, the function is being called.
this.flag = true;
this.inner(); // executes this.flag = false;
if (this.flag === false) { // ... types 'true' and 'false'. - WHY?