Chris Stryczynski
Chris Stryczynski

Reputation: 33901

How do I use Promises with TypeScript when targeting es5?

Promise.all<any, any>(ajaxRequests).then(()=> {
    console.log("done");
});

The above code gives the following compiler error:

TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

I'm not familiar with what this compiler lib option is and what implications it has if I were to change it.


I'm trying to target older browsers and need to support es5 I believe. I assume this can be done by transpiling/polyfilling? My typescript config is:

{
    "compilerOptions": {
        "sourceMap": true,
        "target": "es5",
        "declaration": true,
        "removeComments": false,
        "module" : "commonjs",
        "moduleResolution": "node",
        "resolveJsonModule": true,
    },
    "include": [
        "src/*"
    ],

    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

Upvotes: 3

Views: 2009

Answers (1)

pushkin
pushkin

Reputation: 10209

Add the following to your compiler options:

"lib": [
        "dom",
        "es5",
        "es2015.promise"
    ]

The lib options are described in more detail here.

Here's an explanation of the difference between target and lib.

That being said, if using es6 is acceptable to you, then I think you can just set target to "es6" instead of messing with lib.

Upvotes: 6

Related Questions