WebStorm,ES5/ES3中的异步函数或方法需要"Promise"构造函数 [英] WebStorm, An async function or method in ES5/ES3 requires the 'Promise' constructor

查看:106
本文介绍了WebStorm,ES5/ES3中的异步函数或方法需要"Promise"构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用WebStorm IDE用打字稿(ES6)编写测试.例如:

I try to write tests in typescript (ES6) using WebStorm IDE. E.g.:

// Imports...

describe('Message', () => {
    const server = express();
    server.use(bodyParser.json());

    const messageService = { findAll: () => ['test'] };

    beforeAll(async () => {
        const module = await Test.createTestingModule({
            modules: [MessageModule],
        })...
    });

    // Tests...
});

但是WebStorm IDE在async () =>

However WebStorm IDE shows following error at async () =>

TS2705:ES5/ES3中的异步功能或方法需要Promise 构造函数.确保您有一个承诺声明 构造函数,或在--lib选项中包含ES2015.

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.

我的tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "allowJs": true,
    "outDir": "./dist"
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

我阅读了 ts ES5/ES3中的异步函数或方法需要"Promise"构造函数,并尝试添加

"lib": [ "es2015" ]

但是它没有任何作用.有什么想法吗?

however it does not have any effect. Any ideas what's wrong?

推荐答案

添加

"lib": [ "es2015" ]

tsconfig.json 可以解决此问题. 但是,您的spec文件似乎未包含在您的 tsconfig.json 中(请检查"include":[]"exclude":[]值).因此,Typescript服务必须为您的文件使用其他 tsconfig.json (如果找不到包含您规格的 tsconfig.json 文件,则可能是默认设置) 要解决此问题,请确保在用于规范文件处理的配置中指定lib属性

to tsconfig.json should fix the issue. However it seems that your spec files are not included in your tsconfig.json (check "include":[] and "exclude":[] values). So the Typescript service must be using a different tsconfig.json for your files (may be a default one, if no tsconfig.json files that include your specs can be found) To fix the issue, make sure to specify the lib property in config that is used for your spec files processing

这篇关于WebStorm,ES5/ES3中的异步函数或方法需要"Promise"构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆