Reputation: 8916
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?
Upvotes: 160
Views: 136161
Reputation: 1437
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:
Upvotes: 2
Reputation: 1305
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
Upvotes: 1
Reputation: 11
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
Upvotes: 1
Reputation: 2089
In my case, I had simply accidentally deleted my "return" statement in the function, and this error was showing until I put it back.
Upvotes: 11
Reputation: 986
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"
]
}
Upvotes: 4
Reputation: 767
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
Upvotes: 0
Reputation: 801
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"
]
Upvotes: 7
Reputation: 137
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;
}
Upvotes: 2
Reputation: 2951
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
Upvotes: 15
Reputation: 1742
You could also use the "lib": "es2015.promise" for that specific error
Upvotes: 1
Reputation: 15589
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
Upvotes: 231
Reputation: 41
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.
Upvotes: -3
Reputation: 2123
Try this package which contains type definitions for es6-promise
npm install --save @types/es6-promise
Upvotes: 33