Javascript 财产';全部结算';不存在于类型';承诺建造商';。ts(2339)
我正在尝试将API与TypeScript一起使用。代码如下:Javascript 财产';全部结算';不存在于类型';承诺建造商';。ts(2339),javascript,typescript,es6-promise,Javascript,Typescript,Es6 Promise,我正在尝试将API与TypeScript一起使用。代码如下: server.test.ts: it('如果QPS>50,则应部分成功',异步()=>{ 常量请求:任意[]=[]; for(设i=0;i
server.test.ts
:
it('如果QPS>50,则应部分成功',异步()=>{
常量请求:任意[]=[];
for(设i=0;i<51;i++){
请求推送(rp)http://localhost:3000/place'));
}
等待承诺。所有已解决(请求);
// ...
});
但TSC抛出了一个错误:
类型“PromiseConstructor”上不存在属性“allSettled”。ts(2339)
我已经将这些值添加到tsconfig.json
中的lib
选项中:
{
"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"
]
// ...
}
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"
]
// ...
}
类型脚本版本:“类型脚本”:“^3.7.3”
那么,我该如何解决这个问题呢?我知道我可以使用另一个模块,但我很好奇如何在本机上使用TypeScript。仅在1月份合并,显然将在TypeScript 3.8中发布
作为临时解决方案,您可以自己为函数声明mock-ish类型:
declare interface PromiseConstructor {
allSettled(promises: Array<Promise<any>>): Promise<Array<{status: 'fulfilled' | 'rejected', value?: any, reason?: any}>>;
}
声明接口PromiseConstructor{
AllSetted(承诺:数组):承诺;
}
它是ES2020,处于第4阶段,因此如果没有polyfill,就无法在任何地方使用。它被输入并合并到一个文件中。试着安装最新的@types/node包,看看是否能把它拉进来
更新:看起来它将添加es2020.promise
到libs,当它着陆时
更新:
npmitypescript@3.8.0-测试版
woot-woot 要在Linux上运行,我需要最新的typescript版本:
npm install -g typescript@latest
然后在tsconfig中,您当前需要ES2020.Promise库。我的完整配置:
{
"compilerOptions": {
"sourceMap": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"esModuleInterop": true,
"allowJs": true,
"outDir": "./dist",
"lib": [
"ES2020.Promise",
]
},
"include": [
"./src"
],
"exclude": [
"./node_modules",
"./build"
],
"compileOnSave": true,
"parserOptions": {
"ecmaFeatures": {
"jsx": true
}
}
}
用法:
const results=wait Promise.allselled(成批promises)代码>将其与旧的Typescript版本一起使用的变通方法
await (Promise as any).allSettled(promises);
有一个垫片库提供了相同的功能(),但请注意,它需要显式导入(var allselled=require('promise.allselled');
)
如果您使用的是typescript,则等效的类型化包为,但请注意,类型名称与官方ES2020实现不匹配,因此,如果您需要精确的插入式替换,则需要添加新的TS文件以别名这些类型,并在全局命名空间中提供:
import { PromiseRejection, PromiseResolution, PromiseResult } from 'promise.allsettled';
// https://stackoverflow.com/a/59499895/323177
export {}
// TechDebt: Remove once project is migrated to ES2020
// Global type aliases are required because the promise.allsettled shim doesn't match the types of the actual
// ES2020 implementation
declare global {
export type PromiseFulfilledResult<T> = PromiseResolution<T>;
export type PromiseRejectedResult = PromiseRejection<any>;
export type PromiseSettledResult<T> = PromiseResult<T, any>;
export class PromiseConstructor {
/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T extends readonly unknown[] | readonly [unknown]>(values: T):
Promise<{ -readonly [P in keyof T]: PromiseSettledResult<T[P] extends PromiseLike<infer U> ? U : T[P]> }>;
/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T>(values: Iterable<T>): Promise<PromiseSettledResult<T extends PromiseLike<infer U> ? U : T>[]>;
}
}
从“promise.allsolited”导入{promisereexecution,PromiseResolution,PromiseResult};
// https://stackoverflow.com/a/59499895/323177
导出{}
//TechDebt:项目迁移到ES2020后删除
//全局类型别名是必需的,因为promise.alls与实际类型不匹配
//ES2020实施
宣布全球{
导出类型PromiseFulfilledResult=PromiseResolution;
导出类型PromisejectedResult=PromiseRejection;
导出类型PromiseSettledResult=PromiseResult;
导出类承诺构造函数{
/**
*创建一个承诺,当所有
*所提供的承诺中的任何一项都应予以解决或拒绝。
*@param重视一系列承诺。
*@返回一个新的承诺。
*/
所有已结算(值:T):
承诺;
/**
*创建一个承诺,当所有
*所提供的承诺中的任何一项都应予以解决或拒绝。
*@param重视一系列承诺。
*@返回一个新的承诺。
*/
AllSetted(值:Iterable):承诺;
}
}
您如何判断它何时发布?@JoshWulf查看该提交的标记,您可以看到它们是针对3.8的。一些谷歌搜索可以将你带到显示日程安排的地方,我的日程安排是3.9.5,但它仍然没有这种类型,我的env有什么问题吗?@oskar132确保你的tsconfig.json
在lib
数组中有es2020
,在tsconfig.json
中更改为es2020
后,我不得不关闭并重新打开VSCode,但它可以工作。谢谢@jmzagorski!最直截了当的一句话!如果没有必要,请不要这样做。这是一个让TS编译器通过的黑客程序。相反,您应该使用垫片库,例如,以避免修改生产代码