Angular 11 默认 TypeScript 配置:target 'es2015'、lib 'es2018',但没有 polyfill?

Angular 11 default TypeScript configuration: target 'es2015', lib 'es2018', but no polyfill?

引导新的 Angular 11 应用程序将生成以下 tsconfig.json 文件:

{
  "compileOnSave": false,
  "compilerOptions": {
    ... // Options not relevant for this question.
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  }

它还会生成一个 polyfills.ts 文件,其中只有 zone.js enabled/uncommented。

我的问题是:由于我们编译为 es2015,但使用 es2018 类型定义(例如 async iterablespromise.finally 等),这怎么可能如果没有提供相关的 polyfill 是否安全? 为什么 Angular 11 CLI 不对 lib 使用 es2015

angular 个 polyfill 自动生成为 polyfills-es5.js。在这个文件中是 angular 内部需要工作的 polyfill。如果您的代码使用的功能不属于 angular 使用的功能,则需要将其添加到 polyfills.ts.

因为目标是es2015,typescript会自动转换一些es2018的特性,比如async的东西。

es2015 自动包含为 lib,因为它是 es2018 的子集(我相信)。

旁注: 如果你将 es2018 用作 target,angular 会在编译时给出警告,因为原生 async/await 无法被 zone.js 捕获。如果你在任何地方都使用 OnPush

,这不是问题