异常行为,找不到名称“Array”

Has*_*aan 5 typescript ecmascript-6 visual-studio-code angular

我正在尝试使用 Angular 7 中的数组功能,但我遇到了错误:cannot find name 'Array'. 同样,我无法使用简单的数组方法,如推送和弹出。

这是样本

Angular7 不允许我初始化变量,例如:

list: Array<number> = [1, 2, 3];

根本原因是什么以及如何解决?

更新:tsconfig.js

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

此外,代码编译没有错误。

代码编译成功

神秘的解决方案:我设法通过更新 Visual Studio 代码来解决编译错误。该问题尤其与 Visual Studio 代码更新有关。

小智 2

您的特定 Angular 设置可能存在问题。看看这个问题,其他人也有类似的问题:Angular and Typescript: Can't find names

看看这是否对您有帮助:npm install --save-dev @types/core-js。它应该显式安装类型定义。

希望能帮助到你!