Arrays Typescript-具有不同返回值的承诺的动态数组

Arrays Typescript-具有不同返回值的承诺的动态数组,arrays,typescript,types,promise,promise.all,Arrays,Typescript,Types,Promise,Promise.all,有人能帮我解决这个代码的问题吗? 我找不到合适的类型来输入该承诺。所有的都在底部调用。 它还尝试了Promise.all(ops)但是PullRequests[]不能是可选的 函数等待(时间:数字,响应:Twait):承诺{ 返回新承诺(resolve=>setTimeout(()=>resolve(response),time)) } 接口服务{ 名称:字符串; id:编号; } 异步函数getServices():Promise{ const response=wait wait(400,{

有人能帮我解决这个代码的问题吗? 我找不到合适的类型来输入该
承诺。所有的
都在底部调用。
它还尝试了
Promise.all(ops)
但是
PullRequests[]
不能是可选的

函数等待(时间:数字,响应:Twait):承诺{
返回新承诺(resolve=>setTimeout(()=>resolve(response),time))
}
接口服务{
名称:字符串;
id:编号;
}
异步函数getServices():Promise{
const response=wait wait(400,{“id”:200});
返回[{name:“service”,id:response.id}]
}
接口请求{
prType:字符串;
githubId:数字;
}
异步函数getPrs():Promise{
const response=wait wait(400,{“githubId”:200});
return[{prType:“foo”,githubId:response.githubId}]
}
异步函数main():Promise{
常量操作:[类似承诺,类似承诺?]=[getServices()]

if(Math.random()>0.5){/看起来像是TS库定义中的一个已知问题,根据。目前,库硬编码了一系列不包含可选元素的特定元组类型。已经采取了一些措施来解决这个问题,但显然
PromiseLike
类型以及它们如何与
wait
交互非常复杂,非修复此问题的拉取请求中有e个已被接受。说明切换此选项可能会破坏依赖于当前定义的现有现实世界代码

除非向上游解决此问题,否则您可以使用为
Promise.all()
添加自己的签名,该签名仅用于在传入数组或元组的元素上展开Promise:

interface PromiseConstructor {
  all<T extends readonly any[]>(
    values: { [K in keyof T]: T[K] | PromiseLike<T[K]> }
  ): Promise<T>;
}
接口承诺构造函数{
全部(
值:{[K in keyof T]:T[K]| PromiseLike}
):承诺;
}
您可以测试它是否满足您的要求:

const promiseAllOps = Promise.all(ops); // no error now  
// const promiseAllOps: Promise<[Service[], (PullRequest[] | undefined)?]>
const awaitPromiseAllOps = await promiseAllOps;
// const awaitPromiseAllOps: [Service[], (PullRequest[] | undefined)?]
const [services, prs] = awaitPromiseAllOps;
services; // Service[]
prs; // PullRequest[] | undefined
const promiseAllOps=Promise.all(ops);//现在没有错误
//承诺者:承诺
const await promiseAllOps=等待承诺;
//const waitpromiseallops:[服务[],(PullRequest[]|未定义)?]
const[服务,prs]=等待承诺;
服务;//服务[]
prs;//PullRequest[]|未定义

const promiseAllOps = Promise.all(ops); // no error now  
// const promiseAllOps: Promise<[Service[], (PullRequest[] | undefined)?]>
const awaitPromiseAllOps = await promiseAllOps;
// const awaitPromiseAllOps: [Service[], (PullRequest[] | undefined)?]
const [services, prs] = awaitPromiseAllOps;
services; // Service[]
prs; // PullRequest[] | undefined