TypeScript 中的双等号 (==)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13952941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:57:29  来源:igfitidea点击:

Double Equals (==) in TypeScript

typescript

提问by RichardTowers

I've been writing some unit tests in TypeScript. The example QUnit test contains:

我一直在用 TypeScript 编写一些单元测试。示例 QUnit 测试包含:

  ok( 1 == "1", "Passed!" );

The tsccompiler claims that:

tsc编译器声称:

Operator '==' cannot be applied to types 'number' and 'string'

运算符“==”不能应用于“数字”和“字符串”类型

And exits with status 1(although it does correctly produce the JS).

并以状态退出1(尽管它确实正确生成了 JS)。

The spec says:

规范说:

The <, >, <=, >=, ==, !=, ===, and !== operators

These operators require one operand type to be identical to or a subtype of the other operand type. The result is always of the Boolean primitive type.

<、>、<=、>=、==、!=、=== 和 !== 运算符

这些运算符要求一种操作数类型与另一种操作数类型相同或成为另一种操作数类型的子类型。结果总是布尔基元类型。

So it looks like the warning / error is correct. Doesn't this rather defeat the point of the type coercing ==operator though? Is there ever a valid use case for using ==in TypeScript that won'tproduce this warning?

所以看起来警告/错误是正确的。不过,这难道不是打败了类型强制==运算符的要点吗?是否有==在 TypeScript 中使用不会产生此警告的有效用例?

采纳答案by raina77ow

At least one probable scenario for ==(i.e., with type coercion) in TypeScript is when one of the expected operands is of Any type:

==TypeScript 中(即使用类型强制)至少有一种可能的情况是当预期的操作数之一是 Any 类型时:

A type S is a subtype of a type T, and T is a supertype of S, if one of the following is true: [...]

T is the Any type.

类型 S 是类型 T 的子类型,而 T 是 S 的超类型,如果以下条件之一为真: [...]

T 是 Any 类型。

Now you probably see the picture: any function that has an Any param can safely (well, more-o-less; all the common gotchas of ==are still applied here) compare it with value of any set type with ==.

现在您可能会看到图片:任何具有 Any 参数的函数都可以安全地(好吧,更多 - 更少;所有常见问题==仍然适用于此处)将其与任何集合类型的值进行比较==

回答by Fenton

Here are some common TypeScript examples that are allowed by the compiler and make use of ==.

以下是一些编译器允许并使用==.

var a: string = "A";
var b: Object = "A";

if (a == b) {
    alert("Example 1");
}

var c: any = "1";
var d: number = 1;

if (c == d) {
    alert("Example 2");
}

var e: any = "E";
var f: string = "E";

if (e == f) {
    alert("Example 3");
}

回答by Dan Maharry

One of the points of TypeScript means that we write clearer JavaScript. Do something like 1 == "1" and it won't work unless you explicitly cast it or use ToString() / ParseInt() anyway depending on whether you're expecting to compare strings or numbers.

TypeScript 的要点之一意味着我们可以编写更清晰的 JavaScript。执行诸如 1 == "1" 之类的操作,除非您显式转换它或使用 ToString() / ParseInt() 否则它不会起作用,这取决于您是希望比较字符串还是数字。

You can use Any so that variables behave as usual dynamic JavaScript variables but then you're losing sight of the point of TS which is to benefit from a strong typing / type inference system that helps us not fall foul of the many JavaScript gotchas that exist because of its automatic type coercion rules.

您可以使用 Any 使变量表现得像通常的动态 JavaScript 变量,但随后您就忽略了 TS 的重点,即受益于强大的类型/类型推断系统,该系统可帮助我们避免与现有的许多 JavaScript 问题发生冲突因为它的自动类型强制规则。