Typescript is basically throwing an error there because it’s bad code.
true
will never ever equal false
. Typescript knows this, and tells you to fix your code.
Since these are constant values, they’re considered to be of the types true
and false
. The error message might be slightly confusing, but it’s correct, while giving a bit of an insight into the Typescript engine.
You’ll get a similar error for this code:
if (1 === 2) {}
Error:
Operator ‘===’ cannot be applied to types ‘1’ and ‘2’.
Comparing constants like these throws that error:
false === true; // Operator '===' cannot be applied to types 'false' and 'true'.
1 === 2; // Operator '===' cannot be applied to types '1' and '2'.
"a" === "b"; // Operator '===' cannot be applied to types '"a"' and '"b"'.
Object.is
is completely different from ===
. Object.is
performs no type coercion, and only compares both parameter’s value. Since TypeScript doesn’t validate those arguments with the function’s functionality, it doesn’t throw an error there.
7
solved operator ‘===’ cannot be applied to types ‘false’ and ‘true’