“所有合法的 JavaScript 都是合法的 TypeScript"是什么意思?意思? [英] What does "all legal JavaScript is legal TypeScript" mean?

查看:16
本文介绍了“所有合法的 JavaScript 都是合法的 TypeScript"是什么意思?意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到人们这样说

所有 JavaScript 代码都是合法的 TypeScript 代码

All JavaScript code is legal TypeScript code

TypeScript 是 JavaScript 的超集

TypeScript is a superset of JavaScript

但是当我写了一些完全合法和合理的 JS 代码时,它具有完全确定性的行为:

But yet when I wrote some perfectly legal and reasonable JS code which has totally deterministic behavior:

var x = "hello".substr("w").toStrig * { m: 3 / true } + window + parseInt(Element).fzq;

ECMAScript 定义 x 应该有值 "NaN[object Window]undefined" 这看起来完全没问题,但我从 TypeScript 中得到了一堆错误!那么,所有 JS 都是 TS"这句话难道不是谎言吗?怎么回事?

ECMAScript defines that x should have the value "NaN[object Window]undefined" which seems totally fine, but I got a bunch of errors from TypeScript! Isn't this statement "all JS is TS" a lie, then? What's the deal?

推荐答案

所有 JavaScript 代码都是合法的 TypeScript 代码

All JavaScript code is legal TypeScript code

这是什么意思:

  • TypeScript 不会改变现有 JavaScript 的语法
  • TypeScript 不会改变现有 JavaScript 的行为
  • TypeScript 确实考虑一些 JS 代码有类型警告,因为这是重点
  • TypeScript does not change the syntax of existing JavaScript
  • TypeScript does not change the behavior of existing JavaScript
  • TypeScript does consider some JS code to have type warnings, because that's the point

TypeScript 将成功解析所有合法的 JavaScript 代码.它将按原样发出此代码(减去降级,例如,如果您的目标是 ES5 或更低版本,则 ES6 箭头函数将转换为等效的 ES5 代码).如果您愿意,您可以忽略此 JS 代码生成的类型警告;类型警告不会阻止 TypeScript 写入输出 .js 文件.

TypeScript will successfully parse all legal JavaScript code. It will emit this code as-is (minus downleveling, e.g. ES6 arrow functions will be converted to the equivalent ES5 code if you are targeting ES5 or lower). You may ignore type warnings generated by this JS code if you like; type warnings will not stop TypeScript from writing the output .js file.

TypeScript 不会改变现有 JavaScript 代码的行为,尽管很多人希望它这样做!任何通过编译器运行的 JS 代码都将与直接运行一样*.

TypeScript does not change the behavior of existing JavaScript code, even though many people wish it did! Any JS code run through the compiler will behave the same as if it were run directly*.

TypeScript 可能对它认为不正确的代码发出类型警告.

TypeScript may issue type warnings on code that it considers incorrect.

不正确是什么意思?请注意,JavaScript 是确定性的.例如,与 C++ 不同,它基本上不可能导致实现定义"的行为.JavaScript 也很少抛出异常;与尝试将对象乘以函数时可能引发异常的其他语言不同,JS 生成 NaN.通过拼写错误的名称访问属性将产生 undefined 而不是运行时错误(让每个人都非常懊恼).因此,这里对不正确"的限制比使您的计算机崩溃"或引发异常"更严格.

What does incorrect mean? Note that JavaScript is deterministic. Unlike C++, for example, it is basically impossible to cause behavior which is "implementation-defined". JavaScript also very rarely throws exceptions; unlike other languages which might raise an exception when trying to multiply an object by a function, JS produces NaN. Accessing a property by a mispelled name will produce undefined instead of a runtime error (much to everyone's chagrin). So the bar for "incorrect" here is intentionally set more strictly than "crashes your computer" or "throws an exception".

例如,一些像这样的代码都是合法 JavaScript,甚至不会抛出异常.TypeScript 认为这些行中的每一行都有错误;这通常被使用它的人认为是积极的:

Some code like this, for example, is all legal JavaScript that doesn't even throw an exception. TypeScript considers each of these lines to have an error; this is typically considered a positive by those who use it:

var x = { } + 3; // Error, can't add objects and numbers
var y = "hello world".substr(1, 2, 3, 4, 5); // Error, too many parameters
var z = { x: 1, x: 2 }; // Error, duplicate property 'x'
var q = x[z]; // Error, indexing by an object doesn't really work...
var u = "hi".lenth; // Error, no property 'lenth' on string

但是规范!

常见的反驳是这样的

But the spec!

A common counterargument goes like this

"ECMAScript 规范定义 Math.max 将其参数强制转换为一个数字,因此调用 Math.max(someString, someOtherString) 应该是合法的)"

"The ECMAScript specification defines that Math.max coerces its arguments to a number, so it should be legal to call Math.max(someString, someOtherString)"

ECMAScript 规范确实明确定义了在运行时发生的强制转换.然而,这个逻辑并没有给我们任何实际的洞察力.从表面上看,这个逻辑说,因为所有参数在运行时都被强制为数字,所以写成应该是合法的

It is true that the ECMAScript specification explicitly defines the coercions that take place at runtime. However, this logic doesn't give us any actual insight. Taken at its face, this logic says that because all parameters are coerced to number at runtime, it should be legal to write

var x = Math.max("hello", window.setTimeout, { });

毕竟,这段代码确实有定义的行为!但这只是只见树木不见森林-对于任何合理的正确"定义来说,这段代码都是正确的,这显然是不可信的.TypeScript 的存在是基于某些 JavaScript 代码不正确并且您想了解它的概念.

After all, this code does have defined behavior! But this misses the forest for the trees - it is plainly implausible that this code is correct for any reasonable definition of "correct". TypeScript's existence is predicated on the notion that some JavaScript code is not correct and you'd like to know about it.

规范描述了发生了什么这一事实对于您是否正在编写正确的程序没有指导意义.医生可以清楚地描述如果你吃了一块石头会发生什么,但这并不意味着石头就是食物.

The fact that the spec describes what happens is not instructive as to whether or not you're writing a correct program. A doctor can clearly describe what will happen to you if you eat a rock, but that doesn't mean rocks are food.

查看 ECMAScript 规范是一个类别错误,该规范旨在指定行为,并确定它实际上是一个描述正确代码的规范性文档.如果您愿意,您可以使用规范定义的任何内容都应该没问题"的虚无主义理论,并使用从不发出类型警告的 TypeScript 变体.这种变体被广泛使用,被称为JavaScript".

It's a category error to look at the ECMAScript specification, which is designed to specify behavior, and decide that it is in fact a normative document which describes correct code. If you want, you can run with the nihilistic theory that "anything the spec defines should be OK" and use a variant of TypeScript which never issues type warnings. This variant is widely available, and is called "JavaScript".

*:此规则的例外是降级,由于运行时的限制,它可能具有可检测的差异,例如堆栈跟踪可能不同

*: Exceptions to this rule are downlevelings which may have detectable differences due to limitations in the runtime, e.g. stack traces may be different

这篇关于“所有合法的 JavaScript 都是合法的 TypeScript"是什么意思?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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