最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - operator '===' cannot be applied to types 'false' and 'true' - Stac

programmeradmin6浏览0评论

I know this below code is stupid. But in real time it may possible while piling 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..

I know this below code is stupid. But in real time it may possible while piling 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..

Share Improve this question edited Jan 30, 2018 at 14:51 Cerbrus 73k19 gold badges136 silver badges150 bronze badges asked Jan 30, 2018 at 14:23 Ramesh RajendranRamesh Rajendran 38.7k49 gold badges159 silver badges240 bronze badges 9
  • 1 Okay....it can't be applied...what is the question? – Pavlin Petkov Commented Jan 30, 2018 at 14:25
  • 1 FYI Two string objects will never be equal to each other - even if they both contain identical characters. – Scott Marcus Commented Jan 30, 2018 at 14:28
  • 1 Why you're suggesting that the error message you're getting for clearly faulty code, is wrong, is beyond me. – Cerbrus Commented Jan 30, 2018 at 14:29
  • @ScottMarcus: "a" === "a" is true. – Cerbrus Commented Jan 30, 2018 at 14:30
  • 2 @ScottMarcus: now you're paring references, not strings. – Cerbrus Commented Jan 30, 2018 at 14:33
 |  Show 4 more ments

2 Answers 2

Reset to default 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 pletely different from ===. Object.is performs no type coercion, and only pares both parameter's value. Since TypeScript doesn't validate those arguments with the function's functionality, it doesn't throw an error there.

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.

发布评论

评论列表(0)

  1. 暂无评论