operator'==='不能应用于类型'false'和'true' [英] operator '===' cannot be applied to types 'false' and 'true'

查看:205
本文介绍了operator'==='不能应用于类型'false'和'true'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道下面的代码是愚蠢的。但实时可能在使用相同类型的两个不同数据进行编译时。

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




运算符'==='无法应用类型'false'和'true'

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

但是 Object.is()接受这个不同的数据没有任何错误,它返回 false

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

我知道差异。但为什么打字机语法错误同时为什么 Object.is()没有抛出该错误。

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?

运算符'==='无法应用于类型'false'和'true 。它应该像运算符'==='不能应用于类型'Boolean'和'Boolean'

如果消息错误,那么它在任何升级版本中都解决了吗? I
m使用typescript 2.0.3版本。

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

以下情况出现此问题


  • 1

  • 1

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


  • 2

  • 2

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


  • 等..

    推荐答案

    Typescript基本上是在那里抛出错误,因为它的代码不好。

    Typescript is basically throwing an error there because it's bad code.

    true 永远不会等于 false 。 Typescript知道这一点,并告诉你修改代码。

    true will never ever equal false. Typescript knows this, and tells you to fix your code.

    由于这些是常量值,因此它们被认为属于类型 false 。错误消息可能有点令人困惑,但它是正确的,同时对Typescript引擎有一些了解。

    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) {}
    

    错误:


    运算符'==='无法应用于类型'1'和'2'。

    比较这些常量抛出该错误

    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 =完全不同== Object.is 执行 no 类型强制, 比较两个参数的。由于TypeScript不会使用函数的功能验证这些参数,因此它不会在那里引发错误。

    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.

    这篇关于operator'==='不能应用于类型'false'和'true'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆