typescript ts ES5/ES3 中的异步函数或方法需要 'Promise' 构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43555378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ts An async function or method in ES5/ES3 requires the 'Promise' constructor
提问by Sedric Heidarizarei
Hello I'm Using async/await in my TypeScript Project, But I Get this log:
你好,我在我的 TypeScript 项目中使用 async/await,但我得到了这个日志:
[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.
[ts] ES5/ES3 中的异步函数或方法需要“Promise”构造函数。确保您有“Promise”构造函数的声明或在您的--lib
选项中包含“ES2015” 。
How Can I Solve That?
我该如何解决?
回答by unional
As the error message says, add lib: es2015
to your tsconfig.json
正如错误消息所说,添加lib: es2015
到您的 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.
WebStorm 等 JetBrains IDE 默认使用自己的实现。确保将其配置为使用 TypeScript 语言服务。
For Visual Studio, the project files and tsconfig.json
are mutually exclusive. You will need to configure your project directly.
对于 Visual Studio,项目文件 和tsconfig.json
是互斥的。您将需要直接配置您的项目。
https://github.com/Microsoft/TypeScript/issues/3983#issuecomment-123861491
https://github.com/Microsoft/TypeScript/issues/3983#issuecomment-123861491
回答by Jeff Hernandez
Try this package which contains type definitions for es6-promise
试试这个包含 es6-promise 类型定义的包
npm install --save @types/es6-promise
npm install --save @types/es6-promise
回答by Ivandro Ismael Gomes Jao
If you are on VS, delete the tsconfig.json and right click the project in Solution Explorer, then click on Properties->TypeScript Build in Generalchange the followings
如果你在VS上,删除tsconfig.json并在解决方案资源管理器中右键单击该项目,然后在General中单击Properties->TypeScript Build更改以下内容
ECMAScript version: ECMAScript 6
Module System: ES2015
ECMAScript 版本:ECMAScript 6
模块系统:ES2015
回答by Rannie Aguilar Peralta
You could also use the "lib": "es2015.promise" for that specific error
你也可以使用 "lib": "es2015.promise" 来处理那个特定的错误
回答by 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.
VS2019 似乎无法识别 tsconfig.json 文件,因此 LIB 选项不会更改应用程序。这是一种为 typescript 添加 PROMISE 以接受 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;
}
回答by 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).
对我来说,错误发生在文件src/tests
夹内的测试文件中。由于我直接ts-node
用于测试.ts
文件,因此我src/tests/*
在我的tsconfig.json
. 一旦我删除了该行,错误就消失了(这最终是有道理的)。
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
按照已接受的答案中的概述正确配置您的选项。我的tsconfig.json --lib
选择如下:
"lib": [
"es2018",
"dom"
]
回答by 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:
我在 Angular 4 项目中使用 VS2017 v15.8.2 和 Typescript 2.4.2(在我的解决方案中的类库项目下,而不是打字稿项目)。通过禁用JavaScript 语言服务,我能够删除 VS 中的错误/警告:
Options => Text Editor => JavaScript/TypeScript => Language Service
选项 => 文本编辑器 => JavaScript/TypeScript => 语言服务
Restart VS.
重启VS。
Hope this helps.
希望这可以帮助。