2
votes

Currently, I am updating my project from angular2 beta15 to rc4. When I am compiling, I am getting an error:

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

My tsconfig.json looks as following:

{
  "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"
  ]
}

in main.ts I also have included:

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

and 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"
  }
}

This error is gone when I change "target": "ES5" to "ES6" in tsconfig.json, but I need to use ES5. I assume the issue comes up when I do not include ///<reference path="../../node_modules/angular2/typings/browser.d.ts"/> However, according to https://github.com/typings/typings/issues/151, we can use typings/index.d.ts instead.

Could you please share you opinion to solve this issue? Thank you very much in advance.

2

2 Answers

2
votes

I encountered this error while trying to run a demo application. Similar to your experience, if I changed the target to "es6" in my tsconfig.json, I had no issues. After running the Angular 2 Quick Start application, which worked, I started to compare the differences in my tsconfig.json file. It came down to one line that I was missing in my tsconfig.json file:

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

After adding that one line to my tsconfig.json file, it fixed the errors. This is what I found in the Angular Typescript Configuration documentation about that line:

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

2
votes

I think that Set is provided by the polyfill for ES6. Angular2 recommends using core-js for this (see 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)

You can specify it within your typings.json file but you need to install typings using the command typings install.