WebStorm,ES5/ES3 中的异步函数或方法需要 'Promise' 构造函数
WebStorm, An async function or method in ES5/ES3 requires the 'Promise' constructor
我尝试使用 WebStorm IDE 在打字稿 (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: 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"
]
}
我阅读了 并尝试添加
"lib": [ "es2015" ]
然而它没有任何作用。有什么想法吗?
添加
"lib": [ "es2015" ]
到tsconfig.json应该可以解决这个问题。
但是,您的规范文件似乎未包含在您的 tsconfig.json 中(检查 "include":[]
和 "exclude":[]
值)。因此 Typescript 服务必须为您的文件使用不同的 tsconfig.json(可能是默认的,如果没有 tsconfig.json 可以找到包含您的规格的文件)
要解决此问题,请确保在用于规范文件处理的配置中指定 lib
属性
{
"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"
并从 "exclude":[]
中删除 **/*.spec.ts
并将其添加到 "include":[]
为我解决了这个问题。
不编辑项目源的解决方案
我在使用 IntelliJ 时遇到了这个问题,通过更改我的 IDE 设置解决了这个问题:
设置 -> 语言和框架 -> TypeScript
然后在 "Options" 字段中添加:
--lib es2015
在 compilerOptions 下的 tsconfig.json 中添加 "lib": [ "es2015" ] as
{
"compilerOptions": {
"lib": [ "es2015" ]
}
}
我尝试使用 WebStorm IDE 在打字稿 (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: 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"
]
}
我阅读了
"lib": [ "es2015" ]
然而它没有任何作用。有什么想法吗?
添加
"lib": [ "es2015" ]
到tsconfig.json应该可以解决这个问题。
但是,您的规范文件似乎未包含在您的 tsconfig.json 中(检查 "include":[]
和 "exclude":[]
值)。因此 Typescript 服务必须为您的文件使用不同的 tsconfig.json(可能是默认的,如果没有 tsconfig.json 可以找到包含您的规格的文件)
要解决此问题,请确保在用于规范文件处理的配置中指定 lib
属性
{
"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"
并从 "exclude":[]
中删除 **/*.spec.ts
并将其添加到 "include":[]
为我解决了这个问题。
不编辑项目源的解决方案
我在使用 IntelliJ 时遇到了这个问题,通过更改我的 IDE 设置解决了这个问题:
设置 -> 语言和框架 -> TypeScript
然后在 "Options" 字段中添加:
--lib es2015
在 compilerOptions 下的 tsconfig.json 中添加 "lib": [ "es2015" ] as
{
"compilerOptions": {
"lib": [ "es2015" ]
}
}