您好,我在我的 TypeScript 项目中使用 async/await,但我得到了这个日志:
[ts] ES5/ES3 中的异步函数或方法需要“Promise”构造函数。确保您声明了“Promise”构造函数或在您的 --lib
选项中包含“ES2015”。
我该如何解决?
如错误消息所述,将 lib: es2015
添加到您的 tsconfig.json
// tsconfig.json
{
"compilerOptions": {
"lib": [ "es2015" ]
}
}
更新:如果这对你不起作用,试试这个:
WebStorm 等 JetBrains IDE 默认使用自己的实现。确保将其配置为使用 TypeScript 语言服务。
对于 Visual Studio,项目文件和 tsconfig.json
是互斥的。您将需要直接配置您的项目。
https://github.com/Microsoft/TypeScript/issues/3983#issuecomment-123861491
试试这个包含 es6-promise 类型定义的包
npm install --save @types/es6-promise
npm i -D @types/node@16
也可以。
如果您在 VS 上,请删除 tsconfig.json 并右键单击解决方案资源管理器中的项目,然后单击 Properties->TypeScript Build in General 更改以下内容
ECMAScript 版本:ECMAScript 6
模块系统:ES2015
对我来说,错误发生在 src/tests
文件夹内的测试文件中。由于我使用 ts-node
直接测试 .ts
文件,因此我在 tsconfig.json
中排除了 src/tests/*
。一旦我删除了该行,错误就消失了(这最终是有道理的)。
以防万一其他人在他的测试文件中遇到这个问题。
编辑:当然,您需要按照接受的答案中的概述正确配置您的 --lib
选项。我的 tsconfig.json --lib
选项的工作原理如下:
"lib": [
"es2018",
"dom"
]
在 tsconfig.json 中添加“es2015.promise” - 在“lib”中 = 像这样:
{
"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"
]
}
您还可以使用 "lib": "es2015.promise" 来解决该特定错误
VS2019 似乎无法识别 tsconfig.json 文件,因此 LIB 选项不会改变应用程序。这是一种为打字稿添加承诺以接受 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;
}
就我而言,我只是不小心删除了函数中的“return”语句,并且在我放回去之前一直显示这个错误。
当您通过 tsc index.ts --watch
编译或查看您的打字稿文件时,将 --lib
标志及其值添加到它(在我们的例子中为 es2015),它应该看起来像这样
tsc index.ts --watch --lib es2015
为了更好地工作tsc index.ts --watch --lib "es2015, es2016, dom"
-当它无法从 tsconfig.json 读取 dom
我终于设法解决了!
我在终端上的命令:yarn tsc main.ts && nodejs main.js
我的错误消息:
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.
我为解决它所做的就是引用 tsconfig.json 文件。我的 tsconfig.json 文件是这样的:
{
"compilerOptions": {
"target": "ESNext",
"lib": [
"ES2015",
"DOM"
]
}
}
我的终端命令是这样的:yarn tsc -p ./tsconfig.json && nodejs main.js
如果我想运行其他 .ts 文件,我只需这样做:yarn tsc -p ./tsconfig.json && nodejs file_name.js
tsc --target ES6
这就是我的解决方案!
除了建议添加库的其他响应如下
...
"lib": ["es2015"],
...
打字稿服务器也必须重新启动。
在 Visual Studio Code 上,您可以使用命令调色板选项(在 Windows 上默认为 CTRL + Shift + P
)并搜索 Restart TS Server
我在 Angular 4 项目中使用 VS2017 v15.8.2 和 Typescript 2.4.2(在我的解决方案中的类库项目下,而不是 typescript 项目下)。通过禁用 JavaScript 语言服务,我能够删除 VS 中的错误/警告:
选项 => 文本编辑器 => JavaScript/TypeScript => 语言服务
重启VS。
希望这可以帮助。
不定期副业成功案例分享
{ "compilerOptions": { "module": "commonjs", "target": "es5", "noImplicitAny": false, "sourceMap": false } }
这是我的 tsconfig.json 我应该如何编辑?csproj
文件有一些额外的 xml 元素来启用/禁用这些东西。"lib"
数组中包含"dom"
,否则会出现其他错误。