-8

I know this below code is stupid. But in real time it may possible while compiling with two different data with same type.

if (false === true) {}// getting error

operator '===' cannot be applied to types 'false' and 'true'

But Object.is() is accepting this different data without any error and it returning false

I know the difference between them. But why typescript throwing syntax error as same time why Object.is() not throwing that error.

Also this error message is correct? or not?

operator '===' cannot be applied to types 'false' and 'true. it should be like operator '===' cannot be applied to types 'Boolean' and 'Boolean'

If the message is wrong, then it solved in any upgraded versions? I m using typescript 2.0.3 version.

This problem is occurred in this below scenarios

  • 1

    Object.is("string", "string");
    if ("string" === "string1") {
    }
    
  • 2

    Object.is(1, 2);
    if (1 === 2) {
    }
    

etc..

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

2 Answers2

8

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • yeah all of knows that but it still does not mention anything about error message – Arpit Solanki Jan 30 '18 at 14:27
  • Ok, true will never be false, so this code is not very useful. Still syntactically it looks okay, it could run fine, so it's weird that it's rejected and even weirder that Typescript doesn't give a descriptive error (like "unreachable code" or "expression always evaluates to false"). You make it sound like this message and it's cause are obvious and that the code is blatantly faulty, but I'd appreciate some form of 'proof' because it doesn't at all feel obvious. – GolezTrol Jan 30 '18 at 14:32
  • OK. fine. But what about the message? true and false is a value not a type. true&false is a type of Boolean. – Ramesh Rajendran Jan 30 '18 at 14:32
  • `True` and `False` are actually of type `Boolean` – Neoares Jan 30 '18 at 14:36
  • @Neoares: For TypeScript, `true` and `false` can be types on their own. – Cerbrus Jan 30 '18 at 14:37
  • 3
    @Cerbrus Then, since `1` and `2` are constant values, they're also considered to be of types `1` and `2` and you can not compare them? – Neoares Jan 30 '18 at 14:38
  • @Neoares: [Correct.](https://www.typescriptlang.org/play/#src=if%20(false%20%3D%3D%3D%20true)%20%7B%20%7D%0D%0A%0D%0Aif%20(1%20%3D%3D%3D%202)%20%7B%20%7D) – Cerbrus Jan 30 '18 at 14:39
0

Check if you have correctly declare your boolean variable , I think you have declared it to false/true instead of Boolean. I hope this could help someone.

  • No, Typescript allow value as type Please check : https://stackoverflow.com/questions/44560995/why-does-typescript-accept-value-as-a-data-type – Ramesh Rajendran Jun 05 '20 at 08:43