Challenge - 5 Problems
Mapped Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple mapped type
What is the type of
Result after this code runs?Typescript
type Keys = 'a' | 'b' | 'c'; type Result = { [K in Keys]: number }; // What is the type of Result?
Attempts:
2 left
💡 Hint
Mapped types create a new type by looping over keys and assigning the given type.
✗ Incorrect
The mapped type iterates over each key in 'Keys' and assigns the type 'number' to each property, resulting in an object type with keys 'a', 'b', and 'c' all typed as number.
❓ Predict Output
intermediate2:00remaining
Mapped type with readonly modifier
What is the type of
ReadOnlyResult after this code runs?Typescript
type Keys = 'x' | 'y'; type ReadOnlyResult = { readonly [K in Keys]: boolean }; // What is the type of ReadOnlyResult?
Attempts:
2 left
💡 Hint
The readonly modifier makes all properties immutable.
✗ Incorrect
The mapped type applies the readonly modifier to each key in 'Keys', so the resulting type has readonly properties 'x' and 'y' both of type boolean.
❓ Predict Output
advanced2:00remaining
Mapped type with conditional type
What is the type of
ConditionalResult after this code runs?Typescript
type Keys = 'id' | 'name' | 'age'; type ConditionalResult = { [K in Keys]: K extends 'id' ? number : string }; // What is the type of ConditionalResult?
Attempts:
2 left
💡 Hint
Conditional types can assign different types based on the key.
✗ Incorrect
The mapped type checks if the key is 'id'; if yes, it assigns number, otherwise string. So 'id' is number, 'name' and 'age' are string.
❓ Predict Output
advanced2:00remaining
Mapped type with key remapping
What is the type of
Remapped after this code runs?Typescript
type Keys = 'a' | 'b'; type Remapped = { [K in Keys as `prefix_${K}`]: number }; // What is the type of Remapped?
Attempts:
2 left
💡 Hint
Key remapping lets you change property names in mapped types.
✗ Incorrect
The mapped type renames each key by prefixing it with 'prefix_', so keys become 'prefix_a' and 'prefix_b' with type number.
❓ Predict Output
expert3:00remaining
Complex mapped type with modifiers and key remapping
What is the type of
Complex after this code runs?Typescript
type Keys = 'one' | 'two'; type Complex = { +readonly [K in Keys as `_${K}`]-?: K extends 'one' ? string : number }; // What is the type of Complex?
Attempts:
2 left
💡 Hint
Modifiers + and - control readonly and optional flags; key remapping changes keys.
✗ Incorrect
The +readonly adds readonly, -? removes optional, so properties are required and readonly. Keys are renamed with '_' prefix. 'one' maps to string, 'two' to number.