【问题标题】:Cannot find name 'PropertyKey' in index.d.ts (MS Visual Studio and angular)在 index.d.ts 中找不到名称“PropertyKey”(MS Visual Studio 和 Angular)
【发布时间】:2017-12-31 19:52:58
【问题描述】:

我按照这个guide 将角度与.net mvc 一起使用,但我没有成功。我尝试了下面的这些解决方案仍然无法解决问题。我在文件中有错误

TypeScript\node_modules\@types\core-js\index.d.ts

Solution

这是位于项目根目录中的 tsconfig 和 package.json。

  "dependencies": {
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.2",
    "zone.js": "^0.8.14",
    "typings": "^1.3.2"
  }

tsconfig

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "dom",
      "es2017"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

【问题讨论】:

    标签: asp.net-mvc angular typescript


    【解决方案1】:

    依赖于@types/core-js 包。与通过

    显式包含的官方 TypeScript 声明几乎完全是多余的
    "lib": ["dom", "es2017"]
    

    使用

    "target": "es2017"
    

    也意味着与您所做的相同但明确指定 --lib 是一种很好的做法。

    通过与 DOM 和 ECMAScript API 的最新声明重叠,@types/core-js 包(它粗略地描述了 core-js 包提供的 polyfill 的结构)引入了声明冲突的可能性极高。此外,@types/core-js 包的维护远不如您已经引用的 lib.es2017.d.ts 声明。

    由于您列出了依赖项,而 @types/core-js 不在其中,因此它很可能被传递安装。由于它包含全局声明,这本身就有问题。

    要解决此问题,请在您的 tsconfig.json 中明确设置 "types" 属性。这决定了 TypeScript 编译器包含 global @types 包。

    这是一个例子:

    {
      "compilerOptions": {
        "types": [],
    
        "module": "esnext",
        "lib": ["dom", "es2017"]
      }
    }
    

    要在这样的设置中包含 global @types 声明,您必须在新添加的属性中引用它们。

    这是一个例子:

     {
      "compilerOptions": {
        "types": ["bootstrap", "mocha"],
    
        "module": "esnext",
        "lib": ["dom", "es2017"]
      }
    }
    

    注意事项:

    1. 请务必明确依赖任何@types 包,方法是使用包管理器安装它们。依赖传递依赖会导致不确定的构建、不可预测的 CI 故障和普遍的混乱。

    2. @types/core-js 包包含超出 ES2017 范围的 API 声明。如果您需要访问这些类型,请将 --lib 的规范编号值更改为 esnext,如下所示

      {
        "compilerOptions": {
          "lib": ["dom", "esnext"]
        }
      }
      
    3. 您有许多过时且不一致的依赖项。特别是"typings": "^1.3.2"非常旧了,应该更新。 typings 也应该作为 development 依赖项安装,所有包管理器都支持该功能,包括 typings 本身。

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 2016-04-02
      • 2017-02-13
      • 2016-10-03
      • 2018-07-22
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2016-01-24
      相关资源
      最近更新 更多