This should also work the same with earlier versions of Angular 2+.
To get this to work with typescript 2.0.0, I did the following.
npm install --save-dev @types/core-js
tsconfig.json
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"mapRoot": "./",
"module": "es6",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": false,
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"types": [
"core-js"
]
}
More about @types with typescript 2.0.0.
Install Example:
npm install --save-dev @types/core-js
Duplicate Identifier errors
This is most likely because duplicate ecmascript 6 typings are already being imported from somewhere else most likely an old es6-shim.
Double check typings.d.ts
make sure there are no references to es6
. Remove any reference to es6
from your typings directory if you have one.
For Example:
This will conflict with types:['core-js']
in typings.json.
{
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160602141332"
// es6-shim will also conflict
}
}
Including core-js
in the types array in tsconfig.json
should be the only place it is imported from.
Angular CLI 1.0.0-beta.30
If you are using the Angular-CLI, remove the lib array in typings.json
. This seems to conflict with declaring core-js in types.
"compilerOptions" : {
...
// removed "lib": ["es6", dom"],
...
},
"types" : ["core-js"]
Webstorm/Intellij Users using the Angular CLI
Make sure the built in typescript compiler is disabled. This will conflict with the CLI. To compile your typescript with the CLI you can setup a ng serve
configuration.
Tsconfig compilerOptions lib vs types
If you prefer not to install core js type definitions there are some es6 libraries that come included with typescript. Those are used via the lib: []
property in tsconfig.
See here for example: https://www.typescriptlang.org/docs/handbook/compiler-options.html
Note: If --lib is not specified a default library is injected. The default library injected is: ► For --target ES5: DOM,ES5,ScriptHost ► For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost
tl;dr
Short answer either "lib": [ "es6", "dom" ]
or "types": ["core-js"]
can be used to resolve can't find Promise,Map, Set and Iterator
. Using both however will cause duplicate identifier errors.
I also have the same problem--"Promise not found"--when the code wants to create a Promise object.
Tried some solution found on stackoverflow, including the one to take out System.config({ ... }) to form system.js and have it included in index.html.
Finally I solved the problem. The issue is that, in index.html, es6-shim.min.js is included. However, in tsconfig.json, the "target" property under "compilerOptions" has the value of "es5". After I changed it to "es6", error is gone.
Per update es6-shim
isn't supported now, if you have both typings installed together es6-shim
& core-js
together. Remove es6-shim
typing by mentioning in tsconfig.json. You could now refer below core-js
typing for es5
support inside main.ts
///<reference path="./../typings/globals/core-js/index.d.ts"/>
tsconfig.json
exclude: [
"node_modules", //<-- this would be needed in case of VS2015
"node_modules/@typings",
"typings"
]
You just need to set "target"
property to es6
, then all will error go away. And the transpiled code will be in es6
format.
TLDR;
Transpile to es6
Transpile to es5
For the readers:
tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "system",
"moduleResolution": "node",
...
},
"exclude": [
"node_modules",
"jspm_packages"
]
}
Keep in mind uglifyjs does not support es6 at the moment. This could affect you making production bundles.
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
...
},
"exclude": [
"node_modules",
"jspm_packages"
]
}
Install typings, then install es6-shim:
npm install typings --saveDev
typings install dt~es6-shim --global --save
If you go this route, you need to make sure that the typescript compiler can find the .d.ts file.
You have two options:
a. Make sure your tsconfig.json is at the same level as the typings folder.
b. Include a reference in your main.ts file where your angular2 application is bootstrapped.
Note: DO NOT use the exclude flag to exclude typings folder.
project
|-- src
|-- node_modules
|-- package.json
|-- typings
|-- tsconfig.json
As shown in other answers, this file is no longer included by Angular
main.ts:
/// <reference path="../../typings/globals/es6-shim/index.d.ts" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With