TypeScript · Senior · Question 30 of 75
What does the mapped type { [K in keyof T]: Box<T[K]> } produce if T is the tuple [string, number]?
- ✓Correct answer: The tuple
[Box<string>, Box<number>] - 2An object with properties
0,1,lengthand methods - 3An error: mapped types don't work with tuples
Explanation
A mapped type over keyof T, where T is a tuple or array type parameter, maps the elements and preserves tuple-ness, length and modifiers. This lets you type, for example, Promise.all via { [K in keyof T]: Awaited<T[K]> }.