typescript WebStorm,ES5/ES3 中的异步函数或方法需要“Promise”构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46848802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebStorm, An async function or method in ES5/ES3 requires the 'Promise' constructor
提问by Upvote
I try to write tests in typescript (ES6) using WebStorm IDE. E.g.:
我尝试使用 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...
});
However WebStorm IDE shows following error at async () =>
但是 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.
TS2705:ES5/ES3 中的异步函数或方法需要 Promise 构造函数。确保您有 Promise 构造函数的声明或在您的 --lib 选项中包含 ES2015。
My tsconfig.json
:
我的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"
]
}
I read ts An async function or method in ES5/ES3 requires the 'Promise' constructorand tried adding
我读了ts ES5/ES3 中的异步函数或方法需要“承诺”构造函数并尝试添加
"lib": [ "es2015" ]
however it does not have any effect. Any ideas what's wrong?
但是它没有任何效果。任何想法有什么问题?
回答by lena
Adding
添加
"lib": [ "es2015" ]
to tsconfig.jsonshould 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.jsonfor your files (may be a default one, if no tsconfig.jsonfiles 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
到tsconfig.json应该可以解决这个问题。但是,您的规范文件似乎未包含在您的tsconfig.json(检查"include":[]
和"exclude":[]
值)中。因此,Typescript 服务必须为您的文件使用不同的tsconfig.json(可能是默认的,如果找不到包含您的规范的tsconfig.json文件)要解决此问题,请确保lib
在配置中指定属性用于您的规范文件处理
回答by Francesco Borzi
回答by Manu Nair
{
"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"
]
}
adding "lib":["es2015"]
to "compilerOptions"
as per @lena's answer and Removing **/*.spec.ts
from "exclude":[]
and adding it to "include":[]
solved it for me.
加入"lib":["es2015"]
到"compilerOptions"
按@莉娜的答案,并删除**/*.spec.ts
从"exclude":[]
并将其添加到"include":[]
解决了这个问题对我来说。
回答by Ihsan Rehman
Add "lib": [ "es2015" ] in tsconfig.json under compilerOptions as
在 compilerOptions 下的 tsconfig.json 中添加 "lib": [ "es2015" ] 作为
{
"compilerOptions": {
"lib": [ "es2015" ]
}
}