每个JavaScript程序都是TypeScript程序吗? [英] Is every JavaScript program also a TypeScript program?

查看:76
本文介绍了每个JavaScript程序都是TypeScript程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打字稿语言规范说:


每个JavaScript程序也是一个TypeScript程序

Every JavaScript program is also a TypeScript program

现在考虑以下代码:

var i = 5;
i = "five";

这是一个完全有效的javascript,无错误执行。它不是一个有效的TypeScript,它将无法编译。

This is a perfectly valid javascript that will execute with no error. It is NOT a valid TypeScript, and it will fail to compile.

我对上面引用的语句和代码示例的理解明显不匹配。

There is clearly mismatch in my understanding of the quoted statement above and the code example.

请您澄清一下,在上面给出的示例中,规范陈述的真实性是什么。

Could you please clarify, what makes the spec statement true in the context of the example I gave above.

更新

为了解决该陈述不反映程序有效性的论点,让我们这样改写:

To address the argument that the statement does not reflect a program validity, let's rephrase it this way:


每个JavaScript程序也是有效或无效的TypeScript程序

Every JavaScript program is also a valid or invalid TypeScript program


每个JavaScript程序不一定是有效的TypeScript程序

Every JavaScript program is not necessarily a valid TypeScript program

如果作者想说后者,他们为什么不这么说呢?

If the authors wanted to say the latter, why did not they say that?

推荐答案

似乎有人问这个问题好像声明如下:

It seems the question is being asked as if the statement read:


每个JavaScript程序也是一个语义正确的 TypeScript程序

Every JavaScript program is also a semantically correct TypeScript program

该语句将是错误的,但这不是所说的这里。

That statement would be false, but that's not what's being said here.

如果您尝试编译此语法正确的TypeScript代码...

If you try to compile this syntactically correct TypeScript code...

var i = 5;
i = "five";

...您将收到编译错误,因为它不是语义正确-it将字符串分配给隐式键入数字的变量。但是,由于语法正确,除了抛出编译错误之外,编译器仍然会输出具有相同代码的 .js 文件。

...you will get a compile error because it's not semantically correct—it's assigning a string to a variable implicitly typed as a number. However, since it's syntactically correct, the compiler will still output to the .js file with the same code above in addition to throwing the compile error.

那么每个JavaScript程序也都是TypeScript程序吗?是的,但这并不意味着您不会遇到编译错误。

So is every JavaScript program also a TypeScript program? Yes, but that doesn't mean you won't get compile errors.

旁注:您可以通过指定 - noEmitOnError 编译时。

Sidenote: You can stop the compiler from emitting on an error by specifying --noEmitOnError when compiling.

寻址更新

他们可以做的是通过以下方式扩展:

What they could have done is expanded upon this by qualifying that:


每一个语法正确的JavaScript程序也是一个语法正确的TypeScript程序。

Every syntactically correct JavaScript program is also a syntactically correct TypeScript program.

然而,当你在其上下文中查看引用时,你可以看到该段开头介绍的主要思想已经是语法:

However, when you look at the quote in its context you can see the main idea introduced at the start of the paragraph is already about the syntax:


TypeScript是JavaScript的语法糖。 TypeScript语法是Ecmascript 5(ES5)语法的超集。每个JavaScript程序也是一个TypeScript程序。

TypeScript is a syntactic sugar for JavaScript. TypeScript syntax is a superset of Ecmascript 5 (ES5) syntax. Every JavaScript program is also a TypeScript program.

因此,也许作者认为语法正确会重复。

And so, maybe the author thought saying "syntactically correct" would have been repetitious.

这篇关于每个JavaScript程序都是TypeScript程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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