简体   繁体   English

在TypeScript中使用时,ES6功能是否已编译为ES5?

[英]Are ES6 features compiled to ES5 when used within TypeScript?

When I use ES6 features like for example template string, arrow functions, destructuring within a TypeScript file. 当我使用ES6功能时,例如模板字符串,箭头函数,TypeScript文件中的解构。 Afterward I compile the file to normal JavaScript ... 然后我将文件编译为普通的JavaScript ...

Are the ES6 syntax compiled too by the TypeScript compiler? TypeScript编译器是否也编译了ES6语法? Or do I have to use an additional compiler (Babel)? 或者我是否必须使用其他编译器(Babel)?

Are the ES6 syntax compiled too by the TypeScript compiler? TypeScript编译器是否也编译了ES6语法? Or do I have to use an additional compiler (Babel)? 或者我是否必须使用其他编译器(Babel)?

I disagree with the Fylax's answer. 我不同意Fylax的回答。 The TypeScript compiler doesn't require an additional tool for converting the ES6 syntax to ES 3 or 5. TypeScript编译器不需要额外的工具将ES6语法转换为ES 3或5。

The TypeScript compiler tranpiles the new syntax ( let , for … of , arrow functions, rest parameters, etc.) to ES 3 or 5. But it doesn't provide any polyfill by itself. TypeScript编译器将新语法( let for … of ,箭头函数,休止参数等)转换为ES 3或5.但它本身不提供任何polyfill。 In order to use a recent API (like Promise ) on a old VM ES 3 or 5, you have to: 为了在旧的VM ES 3或5上使用最新的API(如Promise ),您必须:

  1. Load a polyfill (like es6-promise ) that makes the API available; 加载一个使API可用的polyfill (如es6-promise );
  2. Say the compiler to use the standard typings for this API. 假设编译器使用此API的标准类型。

It is a robust design option. 这是一个强大的设计选择。 With typeScript, you have to choose carefully the polyfills you need, and to test them on the different browsers you target. 使用typeScript,您必须仔细选择所需的polyfill,并在您定位的不同浏览器上测试它们。

By default, when the target is ES 3 or ES 5, the compiler doesn't use the definitions for the recent ECMAScript API. 默认情况下,当目标是ES 3或ES 5时,编译器不使用最近ECMAScript API的定义。 See the documentation : 查看文档

Note: If --lib is not specified a default library is injected. 注意:如果未指定--lib则会注入默认库。 The default library injected is: 注入的默认库是:

► For --target ES5 : dom,es5,scripthost ►对于--target ES5dom,es5,scripthost

If a polyfill makes an API available, then we can configure the compiler to use it. 如果polyfill使API可用,那么我们可以配置编译器来使用它。 Here is an example of configuration file tsconfig.json for using promises on ES5 VM: 以下是在ES5 VM上使用promises的配置文件tsconfig.json的示例:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "es5", "es2015.promise"]
  }
}

However, Babel can convert a few more features to ES 5 than TypeScript does. 但是,Babel可以将一些功能转换为ES 5,而不是TypeScript。 See the compatibility table from Kangax . 请参阅Kangax的兼容性表

You need additional compilers that downport your code from ES6 to ES5. 您需要其他编译器将代码从ES6下载到ES5。

TypeScript is pretty smart and will do most of the work for you (ie translate let to var or arrow functions to standard functions with right scope and bindings). 打字稿是相当聪明,会做的大部分工作的你(即翻译letvar或箭头功能标准功能与正确的范围和绑定)。

EDIT: as @Paleo pointed out, on 99% you don't need any external compiler as you can provide to TypeScript an extra library (polyfill) which makes everything work fine. 编辑:正如@Paleo指出的那样,99%你不需要任何外部编译器,因为你可以为TypeScript提供一个额外的库(polyfill),它可以使一切工作正常。

You will need an extra compiler on very rare cases when you are not covered neither by transpiler nor by polyfill's. 在非常罕见的情况下,当您既没有被转换器也没有被polyfill覆盖时,您将需要额外的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM