Typed PromiseAll
MediumP2Coding
Declare the type of a function PromiseAll(values) that behaves like Promise.all: it takes an array or tuple whose items may be plain values or promises, and returns a promise of a tuple of the awaited values in the same order.
A tuple literal keeps its length and element types; a non-tuple array of number | Promise<number> gives number[].
Tests your type must pass
const promiseAllTest1 = PromiseAll([1, 2, 3] as const) const promiseAllTest2 = PromiseAll([1, 2, Promise.resolve(3)] as const) const promiseAllTest3 = PromiseAll([1, 2, Promise.resolve(3)]) const promiseAllTest4 = PromiseAll<Array<number | Promise<number>>>([1, 2, 3]) const promiseAllTest5 = PromiseAll([] as const) type cases = [ Expect<Equal<typeof promiseAllTest1, Promise<[1, 2, 3]>>>, Expect<Equal<typeof promiseAllTest2, Promise<[1, 2, number]>>>, Expect<Equal<typeof promiseAllTest3, Promise<[number, number, number]>>>, Expect<Equal<typeof promiseAllTest4, Promise<number[]>>>, Expect<Equal<typeof promiseAllTest5, Promise<[]>>>, ]
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.