Angular2: Resolve TypeScript TS2304 compilation errors in Visual Studio

Standard

I have recently updated my angular2 project and fell into this TS2304 error in Visual Studio 2015

Error    TS2304    Build:Cannot find name 'Promise'.   
Error    TS2304    Build:Cannot find name 'Map'.   
Error    TS2304    Build:Cannot find name 'Iterable'.   
Error    TS2304    Build:Cannot find name 'Set'.   
Error    TS2304    Build:Cannot find name 'Iterator'.
Package.json:
{
  "name": "pms",
  "version": "1.0.0",
  "scripts": {},
  "license": "ISC",
  "dependencies": {
    "@angular/common": "^2.4.9",
    "@angular/compiler": "^2.4.9",
    "@angular/core": "^2.4.9",
    "@angular/forms": "^2.4.9",
    "@angular/http": "^2.4.9",
    "@angular/platform-browser": "^2.4.9",
    "@angular/platform-browser-dynamic": "^2.4.9",
    "@angular/router": "^3.4.9",
    "@angular/upgrade": "^2.4.9",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.10",
    "rxjs": "^5.2.0",
    "systemjs": "^0.20.9",
    "zone.js": "^0.8.0"
  },
  "devDependencies": {
    "@types/core-js": "^0.9.37",
    "typescript": "^2.2.1",
    "typings": "^2.1.0"
  }
}
tsconfig.json:
{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "core-js"
    ]
  }
}
typings.json:
{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160909174046"
  }
}

Solution 1:

If you want to stick to ES5 then then change types/core-js to version “0.9.36” in package.json.

"@types/core-js": "0.9.36",

Solution 2:

In case you want to use latest version of @types-core-js then you need to know that ES6 features like promises aren’t defined when targeting ES5. There are other libraries, but core-js is the javascript library that the Angular team uses. It contains polyfills for ES6. As you can see I have included @types in project, so simple solution is to change target from ‘es5’ to ‘es6’ or ‘es2015’

tsconfig.json:
{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "core-js"
    ]
  }
}
Advertisements

8 thoughts on “Angular2: Resolve TypeScript TS2304 compilation errors in Visual Studio

    • I was aware of this fact before blogging about it, reason was to make safe integration of latest version in new project. Thanks for mentioning it though.

      P.S.
      I have updated the blog with 0.9.36 solution.

      Like

      • jsjslim

        If the target is set to es6, things like arrow functions and async/await will remain untranspiled. This causes problem if the target browser or platform does not support es6.

        Another alternative is to do what the Angular QuickStart does: target:es5 + lib:[dom, es2015].

        Typescript will transpile to es5, but will also use the shipped declarations in lib.es6.d.ts (ie, promises, maps, etc). You still need core-js, but at least @types/core-js can be removed as a devDependency

        Like

  1. Tony CHEN

    ES6 is not working with uglify-js under angular 4. The best way is using the following:

    “target”: “es5”,
    “@types/core-js”: “^0.9.36”,
    “@angular/core”: “^4.1.3”,
    “@types/jasmine”: “^2.5.45”

    All good now for me.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s