WebStorm,ES5 / ES3中的异步函数或方法需要'Promise'构造函数

问题描述 投票:8回答:4

我尝试使用WebStorm IDE在typescript(ES6)中编写测试。例如。:

// 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 () =>上显示以下错误

TS2705:ES5 / ES3中的异步函数或方法需要Promise构造函数。确保您有Promise构造函数的声明或在--lib选项中包含ES2015。

我的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 An async function or method in ES5/ES3 requires the 'Promise' constructor并尝试添加

"lib": [ "es2015" ]

但它没有任何影响。有什么想法有什么不对吗?

typescript async-await webstorm
4个回答
15
投票

添加

"lib": [ "es2015" ]

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


8
投票

Solution without editing the project sources

我有IntelliJ的这个问题,并通过更改我的IDE设置解决:

设置 - >语言和框架 - > TypeScript

然后在“选项”字段中添加:

--lib es2015

enter image description here


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

根据@ lena的回答将"lib":["es2015"]添加到"compilerOptions"并从**/*.spec.ts删除"exclude":[]并将其添加到"include":[]为我解决了它。


0
投票

在compileOptions下的tsconfig.json中添加“lib”:[“es2015”]

{
    "compilerOptions": {
      "lib": [ "es2015" ]
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.