149

Hello I'm Using async/await in my TypeScript Project, But I Get this log:

[ts] An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option.

How Can I Solve That?

Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103

14 Answers14

218

As the error message says, add lib: es2015 to your tsconfig.json

// tsconfig.json
{
  "compilerOptions": {
    "lib": [ "es2015" ]
  }
}

UPDATE: if this doesn't work for you, try this:

JetBrains IDE such as WebStorm, use its own implementation by default. Make sure you configure it to use TypeScript language service instead.

For Visual Studio, the project files and tsconfig.json are mutually exclusive. You will need to configure your project directly.

https://github.com/Microsoft/TypeScript/issues/3983#issuecomment-123861491

unional
  • 14,651
  • 5
  • 32
  • 56
32

Try this package which contains type definitions for es6-promise

npm install --save @types/es6-promise

Jeff Hernandez
  • 2,063
  • 16
  • 20
15

If you are on VS, delete the tsconfig.json and right click the project in Solution Explorer, then click on Properties->TypeScript Build in General change the followings

  • ECMAScript version: ECMAScript 6

  • Module System: ES2015

Conspicuous Compiler
  • 6,403
  • 1
  • 40
  • 52
Ivandro Jao
  • 2,731
  • 5
  • 24
  • 23
7

In my case, I had simply accidentally deleted my "return" statement in the function, and this error was showing until I put it back.

raterus
  • 1,980
  • 20
  • 23
6

For me the error occurred in my testing files inside src/tests folder. Since I use ts-node for testing the .ts files directly, I excluded src/tests/* in my tsconfig.json. As soon as I deleted the line the error was gone (which makes sense in the end).

Just in case someone else is struggling with this in his testing files.

EDIT: Of course you need to configure your --lib option correctly as outlined in the accepted answer. My tsconfig.json --lib option works as following:

"lib": [
    "es2018",
    "dom"
]
bene-we
  • 761
  • 11
  • 23
  • I compile with `tsc` an async function in a ts file on the client, not on the server. I use `document.getElementById` in the function. When using `dom` with `--lib` as param to `tsc` it works. So I think `dom` is needed if you are on the client. – Timo Dec 27 '22 at 15:19
4

Add "es2015.promise" in tsconfig.json - in "lib" = like this :

{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "es5",
            "dom",
            "es2015.promise"
        ],
        "types": [
            "mocha"
        ],
        "module": "commonjs",
        "outDir": "../../../out",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "sourceMap": true,
        "declaration": true
    },
    "exclude": [
        "node_modules",
        "dist",
        "tests"
    ]
}
Guruprasad GV
  • 916
  • 13
  • 18
2

You could also use the "lib": "es2015.promise" for that specific error

2

VS2019 does not seem to recognize the tsconfig.json file, so LIB options will not change the application. This is a way to add the PROMISE for typescript to accept ASYNC AWAIT.

export function AD(first: any, second: any, callBack: any)
{
    const rtn = async (dispatch: any): Promise<void> =>
    {
        await axios.post(TYPE.URI, { // provide a string of a URI to post into
            parm1: first,
            parm2: second
        })
            .then(data => // or you can use response instead of data as a name
            {
                console.log("data from call next");
                console.log(data);
                dispatch({ type: TYPES.AD, payload: data.data });
                if (callBack)
                {
                    callBack(data);
                }
            })
    }
    return rtn;

}
sef
  • 127
  • 1
  • 1
1

tsc --target ES6

This was the solution for me !

rambi
  • 1,013
  • 1
  • 7
  • 25
1

When you compile or watch your typescript file via tsc index.ts --watch add --lib flag to it with its value (in our case es2015), it should look like this
tsc index.ts --watch --lib es2015
for better work tsc index.ts --watch --lib "es2015, es2016, dom" - when it can't read dom from tsconfig.json

ALbert2504
  • 11
  • 2
0

I've finally managed to solve it!
My command on the terminal: yarn tsc main.ts && nodejs main.js My error message:

main.ts:1:16 - error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor.  Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.

1 async function main() {
                 ~~~~


Found 2 errors.

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What I did to solve it, was referencing the tsconfig.json file.
My tsconfig.json file was like this:

{
    "compilerOptions": {
        "target": "ESNext",
        "lib": [
            "ES2015",
            "DOM"
        ]
    }
}

And my terminal command is like this: yarn tsc -p ./tsconfig.json && nodejs main.js If I want to run other .ts file I simply do: yarn tsc -p ./tsconfig.json && nodejs file_name.js

Dr4kk0nnys
  • 697
  • 7
  • 13
0

In addition to the other responses which suggest adding the lib as follows

...
"lib": ["es2015"],
...

the typescript server must also be restarted.

On Visual Studio Code you can do it using the command-palette option (default is CTRL + Shift + P on Windows) and searching for Restart TS Server

Matteo Gaggiano
  • 1,254
  • 15
  • 28
0

I am using JetBrains Rider 2022.2.3 and had the following in my .csproj project file (I am not using the tsconfig.json file):

  <PropertyGroup>
    <TypeScriptTarget>ES6</TypeScriptTarget>    
  </PropertyGroup>

The above meant that I could compile my TypeScript just fine, but Rider was still showing same "use --lib option" error message until I set the target to ES6 for TypeScript Language Service as well. Do it as follows in settings:

enter image description here

Eric Mutta
  • 1,144
  • 1
  • 11
  • 15
-3

I am using VS2017 v15.8.2 and Typescript 2.4.2 in an Angular 4 project (under a class library project in my solution, not a typescript project). I was able to remove the error/warning in VS by disabling the JavaScript language service:

Options => Text Editor => JavaScript/TypeScript => Language Service

Restart VS.

Hope this helps.

Valone
  • 41
  • 7