How to use Typescript with native ES6 Promises

The current lib.d.ts doesn’t have promises in it defined so you need a extra definition file for it that is why you are getting compilation errors.

You could for example use (like @elclanrs says) use the es6-promise package with the definition file from DefinitelyTyped: es6-promise definition

You can then use it like this:

var p = new Promise<string>((resolve, reject) => { 
    resolve('a string'); 
});

edit You can use it without a definition when targeting ES6 (with the TypeScript compiler) – Note you still require the Promise to exists in the runtime ofcourse (so it won’t work in old browsers :))
Add/Edit the following to your tsconfig.json :

"compilerOptions": {
    "target": "ES6"
}

edit 2
When TypeScript 2.0 will come out things will change a bit (though above still works) but definition files can be installed directly with npm like below:

npm install --save @types/es6-promisesource

edit3
Updating answer with more info for using the types.

Create a package.json file with only { } as the content (if you don’t have a package.json already.
Call npm install --save @types/es6-promise and tsc --init. The first npm install command will change your package.json to include the es6-promise as a dependency. tsc –init will create a tsconfig.json file for you.

You can now use the promise in your typescript file var x: Promise<any>;.
Execute tsc -p . to compile your project. You should have no errors.

Leave a Comment