TypeScript 编译器说“ES5/ES3 中的异步函数或方法需要‘Promise’构造函数”;我该如何解决这个问题?

问题描述 投票:0回答:14

您好,我在我的 TypeScript 项目中使用

async
/
await
,但我收到以下日志:

[ts] ES5/ES3 中的异步函数或方法需要“Promise”构造函数。确保您有“Promise”构造函数的声明或在您的

--lib
选项中包含“ES2015”。

我该如何解决这个问题?

typescript async-await
14个回答
225
投票

如错误消息所示,将

lib: es2015
添加到您的 tsconfig.json

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

更新:如果这对您不起作用,请尝试以下操作:

JetBrains IDE,例如 WebStorm,默认使用自己的实现。确保将其配置为使用 TypeScript 语言服务。

对于 Visual Studio,项目文件和

tsconfig.json
是互斥的。您需要直接配置您的项目。

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


32
投票

尝试这个包含 es6-promise 类型定义的包

npm install --save @types/es6-promise


15
投票

如果您使用的是 VS,请删除 tsconfig.json 并右键单击解决方案资源管理器中的项目,然后单击 Properties->TypeScript Build in General 更改以下内容

  • ECMAScript 版本:ECMAScript 6

  • 模块系统:ES2015


11
投票

就我而言,我只是不小心删除了函数中的“return”语句,并且在我将其放回之前一直显示此错误。


6
投票

对我来说,错误发生在

src/tests
文件夹内的测试文件中。由于我使用
ts-node
直接测试
.ts
文件,因此我在
src/tests/*
中排除了
tsconfig.json
。一旦我删除了该行,错误就消失了(这最终是有意义的)。

以防万一其他人在他的测试文件中遇到这个问题。

编辑:当然,您需要按照接受的答案中的概述正确配置您的

--lib
选项。我的
tsconfig.json --lib
选项的工作原理如下:

"lib": [
    "es2018",
    "dom"
]

4
投票

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

2
投票

您还可以使用 "lib": "es2015.promise" 来解决该特定错误


2
投票

VS2019似乎无法识别tsconfig.json文件,因此LIB选项不会更改应用程序。这是一种为打字稿添加 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;

}

2
投票

tsc --target ES6

这就是我的解决方案!


2
投票

我正在使用 JetBrains Rider 2022.2.3,并且我的

.csproj
项目文件中有以下内容(我没有使用
tsconfig.json
文件):

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

上述意味着我可以很好地编译我的 TypeScript,但 Rider 仍然显示相同的“use --lib option”错误消息,直到我将 TypeScript 语言服务的目标设置为 ES6。在设置中进行如下操作:


1
投票

当您通过

tsc index.ts --watch
向其添加
--lib
标志及其值(在我们的例子中为 es2015)编译或观看打字稿文件时,它应该看起来像这样
tsc index.ts --watch --lib es2015

为了更好的工作
tsc index.ts --watch --lib "es2015, es2016, dom"
- 当它无法从 tsconfig.json 读取 dom 时


1
投票

除了建议添加库的其他响应之外

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

打字稿服务器也必须重新启动。

在 Visual Studio Code 上,您可以使用命令调色板选项(Windows 上的默认值为

CTRL + Shift + P
)并搜索
Restart TS Server

来完成此操作

0
投票

我终于解决了!
我在终端上的命令:

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


-3
投票

我在 Angular 4 项目中使用 VS2017 v15.8.2 和 Typescript 2.4.2(在我的解决方案中的类库项目下,而不是 Typescript 项目)。 我可以通过禁用 JavaScript 语言服务来删除 VS 中的错误/警告:

选项 => 文本编辑器 => JavaScript/TypeScript => 语言服务

重启VS。

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.