Typescript-具有不同返回值的promise的动态数组



有人能帮我解决这段代码的问题吗?我找不到合适的类型来放入底部的Promise.all调用。它也尝试了CCD_ 2,但CCD_。。。

function wait<Twait>(time: number, response: Twait): Promise<Twait> {
return new Promise(resolve => setTimeout(() => resolve(response), time))
}
interface Service {
name: string;
id: number;
}
async function getServices(): Promise<Service[]>  {
const response = await wait(400, { "id": 200 });
return [{ name: "service", id: response.id }]
}

interface PullRequest {
prType: string;
githubId: number;
}
async function getPrs(): Promise<PullRequest[]>  {
const response = await wait(400, { "githubId": 200 });
return [{ prType: "foo", githubId: response.githubId }]
}

async function main(): Promise<void> {
const ops: [ PromiseLike<Service[]>, PromiseLike<PullRequest[]>? ] = [getServices()]
if (Math.random() > 0.5) { // <== this is random on purpose.
ops.push(getPrs())
}


const [ services, prs ] = await Promise.all(ops) // This throws a ts compile error (attached below)
console.log("services:")
console.log(services)
console.log("prs:")
console.log(prs)    
}
No overload matches this call. The last overload gave the following error.
Argument of type '[PromiseLike<Service[]>, (PromiseLike<PullRequest[]> | undefined)?]' is not assignable to parameter of type 'Iterable<Service[] | PromiseLike<Service[] | undefined> | undefined>'. 
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. 
Type 'IteratorResult<PromiseLike<Service[]> | PromiseLike<PullRequest[]> | undefined, any>' is not assignable to type 'IteratorResult<Service[] | PromiseLike<Service[] | undefined> | undefined, any>'. 
Type 'IteratorYieldResult<PromiseLike<Service[]> | PromiseLike<PullRequest[]> | undefined>' is not assignable to type 'IteratorResult<Service[] | PromiseLike<Service[] | undefined> | undefined, any>'. 
Type 'IteratorYieldResult<PromiseLike<Service[]> | PromiseLike<PullRequest[]> | undefined>' is not assignable to type 'IteratorYieldResult<Service[] | PromiseLike<Service[] | undefined> | undefined>'. 
Type 'PromiseLike<Service[]> | PromiseLike<PullRequest[]> | undefined' is not assignable to type 'Service[] | PromiseLike<Service[] | undefined> | undefined'. 
Type 'PromiseLike<PullRequest[]>' is not assignable to type 'Service[] | PromiseLike<Service[] | undefined> | undefined'. 
Type 'PromiseLike<PullRequest[]>' is not assignable to type 'PromiseLike<Service[] | undefined>'. 
Type 'PullRequest[]' is not assignable to type 'Service[]'. 
Type 'PullRequest' is missing the following properties from type 'Service': name, id

根据microsoft/TypeScript#28427,TS库定义中似乎存在已知问题。目前,该库对一组不包括可选元素的特定元组类型进行了硬编码。已经做出了一些努力来解决这个问题,但显然PromiseLike类型及其与await的交互方式非常复杂,因此还没有一个解决这个问题的拉取请求被接受。microsoft/TypeScript#37988中的一条评论解释说,切换它可能会破坏依赖于当前定义的现有现实世界代码。

在上游解决之前,您可以使用声明合并为Promise.all()添加自己的签名,该签名仅将promise展开映射到传入数组或元组的元素上:

interface PromiseConstructor {
all<T extends readonly any[]>(
values: { [K in keyof T]: T[K] | PromiseLike<T[K]> }
): Promise<T>;
}

你可以测试它是否符合你的要求:

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

游乐场链接

最新更新