Angular 2 找不到文件名 'Set'

Angular 2 cannot find file name 'Set'

目前,我正在将我的项目从 angular2 beta15 更新到 rc4。编译时出现错误:

path/node_modules/@angular/common/src/directives/ng_class.d.ts(81,35): error TS2304: Cannot find name 'Set'.

我的 tsconfig.json 如下所示:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": false,
    "declaration": true,
    "outDir": "tmp/app"
  },
  "exclude": [
    "node_modules",
    "dist",
    "tmp"
  ]
}

在 main.ts 我还包括:

/// <reference path="../typings/index.d.ts" />
/// <reference path="../typings/tsd.d.ts"/>

和typings.json:

{
  "name": "my-project",
  "dependencies": {},
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160602141332",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160621231320",
    "moment": "registry:dt/moment#2.8.0+20160316155526",
    "moment-node": "registry:dt/moment-node#2.11.1+20160329220348",
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654"
  }
}

当我在tsconfig.json中将"target": "ES5"更改为"ES6"时,这个错误就消失了,但我需要使用ES5。我认为当我不包含 ///<reference path="../../node_modules/angular2/typings/browser.d.ts"/> 时会出现问题。但是,根据 https://github.com/typings/typings/issues/151,我们可以改用 typings/index.d.ts

能否请您分享您的意见来解决这个问题?非常感谢你提前。

我认为 Set 是由 ES6 的 polyfill 提供的。 Angular2 建议为此使用 core-js(参见 https://angular.io/guide/quickstart):

We begin with core-js's ES2015/ES6 shim which monkey patches the global context (window) with essential features of ES2015 (ES6)

您可以在 typings.json 文件中指定它,但您需要使用命令 typings install.

安装 typings

我在尝试 运行 演示应用程序时遇到此错误。与您的经历类似,如果我在 tsconfig.json 中将目标更改为 "es6",我没有遇到任何问题。在 运行 运行 Angular 2 Quick Start 应用程序后,我开始比较 tsconfig.json 文件中的差异。它归结为我在 tsconfig.json 文件中丢失的一行:

"lib": ["es2015", "dom"]

将这一行添加到我的 tsconfig.json 文件后,它修复了错误。这是我在 Angular Typescript 配置文档中找到的有关该行的内容:

lib.d.ts

TypeScript includes a special declaration file called lib.d.ts. This file contains the ambient declarations for various common JavaScript constructs present in JavaScript runtimes and the DOM.

Based on the --target, TypeScript adds additional ambient declarations like Promise if our target is es6.

Since the QuickStart is targeting es5, we can override the list of declaration files to be included:

"lib": ["es2015", "dom"]

Thanks to that, we have all the es6 typings even when targeting es5.

https://angular.io/docs/ts/latest/guide/typescript-configuration.html