New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
type-challenges-solutions/en/medium-promise-all #237
Comments
I dont understand the |
@muzichen short version – yes. Long version... Let's take a type Demo<T> = T
type R0 = Demo<[1, 2, 3]> // type R0 = [1, 2, 3] When adding a keyof operator, you will get a union of “indexes” of the tuple: type Demo<T> = keyof T
type R0 = Demo<[1, 2, 3]> // type R0 = keyof [1, 2, 3] Once we add it to mapped types, what we do is “take keys of the tuple and for each key written into type Demo<T> = { [I in keyof T]: T[I] }
type R0 = Demo<[1, 2, 3]> // type R0 = [1, 2, 3] In this example above, what happened is we copied the tuple. Because we took all the keys from the original one and for each key in there took its value. It is a bit unintuitive, but it works. The thing is that “keys” we are working with here are indexes of the tuple, not the object. |
new test case has been added and this solution is failed. can you update the solution, please? |
@dianrahmaji try to solve it and feel free to leave a comment with the updated solution for other people to see. |
sure. this is the solution that pass all the test cases using a helper type. type MyAwaited<T> = T extends PromiseLike<infer R> ? MyAwaited<R> : T
declare function PromiseAll<T extends any[]>(values: readonly [...T]): Promise<{
[P in keyof T]: MyAwaited<T[P]>
}> |
Could you explain why? |
@imentu type a<T> = {[p in keyof T]: T[p] extends Promise<infer R>? R: T[p]};
type b = a<Array<number|Promise<number>>> The b is |
Here is solution without using mapping types. It uses helper type type PromiseAllType<A extends any[], Acc extends any[] = []> =
A extends [infer FirstElem, ... infer Rest]
? PromiseAllType<Rest, [...Acc, Awaited<FirstElem>]>
: Promise<Acc>
;
declare function PromiseAll<T extends unknown[]>(
values: readonly [...T],
): PromiseAllType<T>; |
Promise.all
This project is aimed at helping you better understand how the type system works, writing your own utilities, or just having fun with the challenges.
https://ghaiklor.github.io/type-challenges-solutions/en/medium-promise-all.html
The text was updated successfully, but these errors were encountered: