即使在 Angular 7 中使用编译器选项作为目标 es6 和 es2017,如何修复编译时错误?

mai*_*ifs 3 node.js typescript angular

当我尝试编译我的 Angular 7 源代码时,我遇到了编译错误。

任何人都可以指导我吗?

我试图在谷歌上找到这些错误并使用了一些文章。

请看我的 tsconfig.json

 {
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [      
      "es2017",
      "dom"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我什至添加了es6但错误仍然相同。

我更新了以下软件包,但错误仍然相同。

 "@types/node": "^10.12.18",
 "ts-node": "^7.0.1",
 "tslint": "^5.12.0"
Run Code Online (Sandbox Code Playgroud)

我使用的是 Angular 7.1.4 和 Material 7.2.0。

错误如下:

Severity    Code    Description Project File    Line    Suppression State
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\cdk\platform\typings\features\input-types.d.ts  9   Active
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\common\src\directives\ng_class.d.ts 48  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\common\src\pipes\keyvalue_pipe.d.ts 47  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\common\src\pipes\keyvalue_pipe.d.ts 50  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\common\src\pipes\keyvalue_pipe.d.ts 51  Active
Error   TS2304  (TS) Cannot find name 'Iterable'.   ClientApp\node_modules\@angular\core\src\change_detection\differs\default_iterable_differ.d.ts  20  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.ClientApp\node_modules\@angular\core\src\change_detection\differs\default_keyvalue_differ.d.ts    31  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\core\src\change_detection\differs\default_keyvalue_differ.d.ts  39  Active
Error   TS2304  (TS) Cannot find name 'Iterable'.   ClientApp\node_modules\@angular\core\src\change_detection\differs\iterable_differs.d.ts 14  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.ClientApp\node_modules\@angular\core\src\change_detection\differs\keyvalue_differs.d.ts   22  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\core\src\di\reflective_provider.d.ts    93  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.ClientApp\node_modules\@angular\core\src\di\reflective_provider.d.ts  93  Active
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\core\src\metadata\ng_module.d.ts    24  Active
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\core\src\metadata\ng_module.d.ts    25  Active
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\core\src\metadata\ng_module.d.ts    28  Active
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.ClientApp\node_modules\@angular\core\src\metadata\ng_module.d.ts  29  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\core\src\render3\jit\compiler_facade_interface.d.ts 117 Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.ClientApp\node_modules\@angular\core\src\render3\jit\compiler_facade_interface.d.ts   118 Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\platform-browser\src\browser\browser_adapter.d.ts   86  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\platform-browser\src\dom\dom_adapter.d.ts   97  Active
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\platform-browser\src\dom\shared_styles_host.d.ts    11  Active
Error   TS2583  (TS) Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\@angular\platform-browser\src\dom\shared_styles_host.d.ts    22  Active
Error   TS2585  (TS) 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\node_modules\rxjs\internal\Observable.d.ts    82  Active
Error   TS2304  (TS) Cannot find name 'Iterable'.   ClientApp\node_modules\rxjs\internal\types.d.ts 35  Active
Error   TS2585  (TS) 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.   ClientApp\node_modules\rxjs\internal\types.d.ts 39  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\src\app\models\DynamicFlatNode.ts 18  Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.ClientApp\src\app\models\DynamicFlatNode.ts   33  Active
Error   TS1219  (TS) Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.    ClientApp\src\app\models\DynamicFlatNode.ts 56  Active
Error   TS1219  (TS) Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.    ClientApp\src\app\models\TodoItemNode.ts    51  Active
Error   TS1219  (TS) Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.    ClientApp\src\app\models\TodoItemNode.ts    114 Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.  ClientApp\src\app\models\TodoItemNode.ts    116 Active
Error   TS2583  (TS) Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.ClientApp\src\app\models\TodoItemNode.ts  119 Active
Run Code Online (Sandbox Code Playgroud)

Mob*_*ent 20

您还可以更改编译期间加载的类型定义,并使目标保持原样,而不是更改目标。这允许您填充所需的新功能,同时仍然转编译到旧目标。

例子:

"compilerOptions": {
  "target": "es2015",
  "module": "esnext",
  "lib": ["dom", "es2018"], <-- additional types loaded here
Run Code Online (Sandbox Code Playgroud)

这使得 TypeScript 知道您使用 ES2018 功能(如promise.finally),并且它将消除如下错误消息: Try changing the 'lib' compiler option to 'es2018' or later.

此设置的一个很酷的事情是您可以在非常细粒度/单独的级别上加载这些类型定义,例如:

"lib": ["ES2015.Iterable", "ES2015.Promise", ...]
Run Code Online (Sandbox Code Playgroud)

您需要确保此功能要么是多填充的,要么确实存在于执行环境中。

查看文档以了解有关支持的类型定义的更多信息:https://www.typescriptlang.org/tsconfig#lib

注意:从 TypeScript 4.5 开始,甚至可以从您自己的类型加载您自己的类型,node_modules而不是依赖 TypeScript 附带的类型:支持来自 node_modules 的 lib


use*_*245 10

tsconfig.json在源代码中搜索文件并尝试将所有target属性的值设置为es2017es6

  • 它不工作 (3认同)

Moh*_*ale 6

我找到了以下解决问题的方法

npm install -D @types/node
Run Code Online (Sandbox Code Playgroud)

  • 当我已经将“目标”设置为 es2017 时,这对我有帮助,谢谢。 (2认同)