Is there anyone out there who can assist me in figuring out what's causing problems with this piece of code? I'm struggling to determine the correct type to use in that Promise.all
call at the end. I attempted using
Promise.all<Services[], PullRequests[]>(ops)
but it seems like PullRequests[]
cannot be optional...
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