刚刚从 9.0 更新到 Angular 10
现在,在我的代码中每次使用可选链接(https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining)都会导致它自己的以下错误实例
ERROR in ./src/app/app.component.ts 39:18
Module parse failed: Unexpected token (39:18)
File was processed with these loaders:
* ./node_modules/@ngtools/webpack/src/index.js
You may need an additional loader to handle the result of these loaders.
| this.title = 'my-app';
| const x = this.GetObject();
> let y = x?.myvar;
| }
我确认此错误仅在我的 tsconfig.base.json 文件中按照以下针对 es2020 时发生,但在针对 es2019 时很好
"target": "es2020", //If set to es2019 then all OK
"module": "es2020",
"lib": [
"es2018", //Error also occurs if this set to es2020
"dom"
],
我确认此错误发生在新生成的新 Angular 10 应用程序中,如下所示,然后将目标更改为 es2020(所以它不是我的代码!)打字稿版本是 3.9.7
npm install -g @angular/cli
ng new my-app
我还注意到,在 es2020 的发行说明中,“可选链接”现在是一项新功能。这让我怀疑“可选链接”的 Typescript 编译不知何故没有与新的 es2020 功能结合?
如何在 es2020 中使用可选链?