PromiseAll
介绍
给函数PromiseAll
指定类型,它接受元素为 Promise 或者类似 Promise 的对象的数组,返回值应为Promise<T>
,其中T
是这些 Promise 的结果组成的数组。
例如
ts
const promise1 = Promise.resolve(3);const promise2 = 42;const promise3 = new Promise<string>((resolve, reject) => {setTimeout(resolve, 100, 'foo');});// 应推导出 `Promise<[number, 42, string]>`const p = PromiseAll([promise1, promise2, promise3] as const)
View on GitHubts
const promise1 = Promise.resolve(3);const promise2 = 42;const promise3 = new Promise<string>((resolve, reject) => {setTimeout(resolve, 100, 'foo');});// 应推导出 `Promise<[number, 42, string]>`const p = PromiseAll([promise1, promise2, promise3] as const)
起点
ts
/* _____________ Your Code Here _____________ */declare functionPromiseAll (values : any): any/* _____________ Test Cases _____________ */constpromiseAllTest1 =PromiseAll ([1, 2, 3] asconst )constpromiseAllTest2 =PromiseAll ([1, 2,Promise .resolve (3)] asconst )constpromiseAllTest3 =PromiseAll ([1, 2,Promise .resolve (3)])constExpected 0 type arguments, but got 1.2558Expected 0 type arguments, but got 1.promiseAllTest4 =PromiseAll <Array <number |Promise <number>>>([1, 2, 3])typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest1 ,Promise <[1, 2, 3]>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest2 ,Promise <[1, 2, number]>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest3 ,Promise <[number, number, number]>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest4 ,Promise <number[]>>>,]
take the challengets
/* _____________ Your Code Here _____________ */declare functionPromiseAll (values : any): any/* _____________ Test Cases _____________ */constpromiseAllTest1 =PromiseAll ([1, 2, 3] asconst )constpromiseAllTest2 =PromiseAll ([1, 2,Promise .resolve (3)] asconst )constpromiseAllTest3 =PromiseAll ([1, 2,Promise .resolve (3)])constExpected 0 type arguments, but got 1.2558Expected 0 type arguments, but got 1.promiseAllTest4 =PromiseAll <Array <number |Promise <number>>>([1, 2, 3])typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest1 ,Promise <[1, 2, 3]>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest2 ,Promise <[1, 2, number]>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest3 ,Promise <[number, number, number]>>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <typeofpromiseAllTest4 ,Promise <number[]>>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
//方案1declare functionPromiseAll <T extends any[]>(values : readonly [...T ]):Promise <{[K in keyofT ]:Awaited <T [K ]>}>
ts
//方案1declare functionPromiseAll <T extends any[]>(values : readonly [...T ]):Promise <{[K in keyofT ]:Awaited <T [K ]>}>
ts
//方案2declare functionPromiseAll <T extends any[]>(values : readonly [...T ]):Promise <{[K in keyofT ]:T [K ] extendsPromise <inferR > ?R :T [K ]}>
ts
//方案2declare functionPromiseAll <T extends any[]>(values : readonly [...T ]):Promise <{[K in keyofT ]:T [K ] extendsPromise <inferR > ?R :T [K ]}>