我正在尝试使用 Promise.allSettled
带有 TypeScript 的 API。代码在这里:server.test.ts
:
it('should partial success if QPS > 50', async () => {
const requests: any[] = [];
for (let i = 0; i < 51; i++) {
requests.push(rp('http://localhost:3000/place'));
}
await Promise.allSettled(requests);
// ...
});
但是 TSC 抛出一个错误:Property 'allSettled' does not exist on type 'PromiseConstructor'.ts(2339)
我已经将这些值添加到
lib
tsconfig.json
中的选项:tsconfig.json
:{
"compilerOptions": {
/* Basic Options */
"target": "ES2015" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"ES2015",
"ES2016",
"ES2017",
"ES2018",
"ES2019",
"ES2020",
"ESNext"
]
// ...
}
typescript 版本:"typescript": "^3.7.3"
那么,我该如何解决呢?我知道我可以使用替代模块,但我对原生使用 TypeScript 感到好奇。
最佳答案
The types for Promise.allSettled()
仅在 1 月份合并,显然将在 TypeScript 3.8 中发布。
作为临时解决方案,您可以自己为函数声明一个模拟类型:
declare interface PromiseConstructor {
allSettled(promises: Array<Promise<any>>): Promise<Array<{status: 'fulfilled' | 'rejected', value?: any, reason?: any}>>;
}
关于javascript - 类型 'allSettled' .ts(2339) 上不存在属性 'PromiseConstructor',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60276013/