简体   繁体   English

如果我使用babel-polyfill,我可以使用Typescript定位ES6吗?

[英]Can I target ES6 with Typescript if I am using babel-polyfill?

I am writing a Typescript / React / Redux application that takes advantage of modern ES6+ features like generators and object spread. 我正在编写一个Typescript / React / Redux应用程序,它利用了现代ES6 +功能,如生成器和对象传播。

I am currently setting my tsconfig.json target to ES5 and also including babel-polyfill in my application. 我目前正在将我的tsconfig.json目标设置为ES5并在我的应用程序中包含babel-polyfill polyfill。

Since ES6 is faster than ES5 in most browsers, I would prefer to target ES6 from my Typescript config. 由于在大多数浏览器中ES6比ES5 ,我更愿意从我的Typescript配置中定位ES6。 However, I am concerned that I will not be able to support as many browsers. 但是,我担心我将无法支持尽可能多的浏览器。

Does babel-polyfill provide the same level of ES5 support as transpiling to an ES5 target? babel-polyfill是否提供与转换为ES5目标相同水平的ES5支持?

Does babel-polyfill provide the same level of ES5 support as transpiling to an ES5 target? babel-polyfill是否提供与转换为ES5目标相同水平的ES5支持?

No. 没有。

babel-polyfill adds missing ES6 runtime classes and methods . babel-polyfill添加了缺少的ES6 运行时类和方法 For example, if the browser running your script does not have Promise or Array.prototype.includes , it adds implementations for those into the global scope so that they exist when your code attempts to use them. 例如,如果运行脚本的浏览器没有PromiseArray.prototype.includes ,它会将这些实现添加到全局范围中,以便在代码尝试使用它们时存在。

It cannot add missing support for ES6 syntax to the browser. 它无法为浏览器添加缺少对ES6 语法的支持。 If your JavaScript code contains ES6 syntax (eg class , ... , or yield ), then an ES5 browser will fail to parse your code . 如果您的JavaScript代码包含ES6语法(例如class...yield ),那么ES5浏览器将无法解析您的代码 No amount of additional JavaScript code at runtime can change how the browser parses your code, so the polyfill doesn't help there. 运行时没有额外的JavaScript代码可以改变浏览器解析代码的方式,因此polyfill对此没有帮助。

Bottom-line: if you want to use ES6 syntax and still support ES5 browsers, you must transform your code to ES5 (either by targeting ES5 from TypeScript, or by transforming using Babel). 底线:如果您想使用ES6语法并且仍支持ES5浏览器,则必须将代码转换为ES5(通过从TypeScript定位ES5,或使用Babel转换)。

In short, no, babel-polyfill doesn't provide the same level of ES5 support as transpiling. 简而言之,不,babel-polyfill不提供与转化相同水平的ES5支持。 The documentation states that it includes a custom regenerator runtime as well as core-js. 文档声明它包括自定义再生器运行时以及core-js。

You can check out core-js to see that it contains the polyfills you need. 您可以查看core-js以查看它包含您需要的polyfill。 However, the object-spread syntax cannot be polyfilled, only transpiled. 但是,对象扩展语法不能被填充,只能被转换。

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

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