ChatGPT解决这个技术问题 Extra ChatGPT

ts An async function or method in ES5/ES3 requires the 'Promise' constructor

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?


u
unional

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


{ "compilerOptions": { "module": "commonjs", "target": "es5", "noImplicitAny": false, "sourceMap": false } } this is my tsconfig.json How Should I edit?
@katopz I did, but VS does not care about it, I found that csproj file has some extra xml elements to enable/disable such things.
I had to restart my IDE (WebStorm) to see the result.
I have that ("lib": ["dom", "es2015"]) but it still complains
I had to include "dom" in the "lib" array as well or I get other errors.
J
Jeff Hernandez

Try this package which contains type definitions for es6-promise

npm install --save @types/es6-promise


If your target is node, npm i -D @types/node@16 works too.
C
Conspicuous Compiler

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


@LukePighetti project in solution-explorer
b
bene-we

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"
]

G
Guruprasad GV

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"
    ]
}

R
Rannie Aguilar Peralta

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


s
sef

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;

}

r
raterus

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


A
ALbert2504

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


D
Dr4kk0nnys

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


r
rambi

tsc --target ES6

This was the solution for me !


M
Matteo Gaggiano

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


V
Valone

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.