为什么 ES2015 类 不能在 TypeScript 中可靠地转译?
Why can't ES2015 classes be reliably transpiled in TypeScript?
我最近在 MDN 上无意中发现了以下声明:
Please note that ES2015 classes cannot reliably be transpiled in Babel 6 or TypeScript targeting legacy browsers. You can either use Babel 7 or the babel-plugin-transform-builtin-classes for Babel 6, and target ES2015 in TypeScript instead of legacy.
这个声明对于 TypeScript 来说是否过时了 > 3.x.x 或者谁能解释一下 TypeScript 转译有哪些缺点?
我在 tsconfig.json
中将 target
设置为 ES5
时没有注意到问题,除了必须填充更多功能(如预期的那样)。
了解这些问题(如果存在的话)对处理自定义元素和 Web 组件非常有帮助。
如 Custom Element polyfill 的 Github 页面中所述,自定义元素被定义为 HTMLElement
class 的扩展,它需要一个新的 ES6 功能,它不能用 ES5 完美模拟 Javascript: super()
/Reflect.construct()
:
The spec requires that an element call the HTMLElement constructor. Typically an ES5 style class would do something like HTMLElement.call(this) to emulate super(). However, HTMLElement must be called as a constructor and not as a plain function, i.e. with Reflect.construct(HTMLElement, [], MyCEConstructor), or it will throw.
我最近在 MDN 上无意中发现了以下声明:
Please note that ES2015 classes cannot reliably be transpiled in Babel 6 or TypeScript targeting legacy browsers. You can either use Babel 7 or the babel-plugin-transform-builtin-classes for Babel 6, and target ES2015 in TypeScript instead of legacy.
这个声明对于 TypeScript 来说是否过时了 > 3.x.x 或者谁能解释一下 TypeScript 转译有哪些缺点?
我在 tsconfig.json
中将 target
设置为 ES5
时没有注意到问题,除了必须填充更多功能(如预期的那样)。
了解这些问题(如果存在的话)对处理自定义元素和 Web 组件非常有帮助。
如 Custom Element polyfill 的 Github 页面中所述,自定义元素被定义为 HTMLElement
class 的扩展,它需要一个新的 ES6 功能,它不能用 ES5 完美模拟 Javascript: super()
/Reflect.construct()
:
The spec requires that an element call the HTMLElement constructor. Typically an ES5 style class would do something like HTMLElement.call(this) to emulate super(). However, HTMLElement must be called as a constructor and not as a plain function, i.e. with Reflect.construct(HTMLElement, [], MyCEConstructor), or it will throw.