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 mapped type with template literals
What is the type of
Result after applying the mapped type with template literals?Typescript
type Flags = 'success' | 'error'; type Result = { [K in `${Flags}Message`]: string; }; // What is the type of Result?
Attempts:
2 left
💡 Hint
Template literals combine each union member with 'Message' suffix.
✗ Incorrect
The mapped type iterates over each member of 'Flags' and appends 'Message' to create keys 'successMessage' and 'errorMessage', both typed as string.
❓ Predict Output
intermediate2:00remaining
Output of nested mapped type with template literals
What is the type of
Options after applying this nested mapped type?Typescript
type Sizes = 'small' | 'medium'; type Colors = 'red' | 'blue'; type Options = { [S in Sizes as `${S}Size`]: { [C in Colors as `${C}Color`]: boolean; }; };
Attempts:
2 left
💡 Hint
Both outer and inner keys use template literals with 'Size' and 'Color' suffixes.
✗ Incorrect
The outer mapped type renames 'small' and 'medium' to 'smallSize' and 'mediumSize'. The inner mapped type renames 'red' and 'blue' to 'redColor' and 'blueColor'.
🔧 Debug
advanced2:00remaining
Identify the error in mapped type with template literals
What error does this TypeScript code produce?
Typescript
type Keys = 'one' | 'two'; type Mapped = { [K in Keys as `${K}Key`]: number; [K in Keys as `${K}Value`]: string; };
Attempts:
2 left
💡 Hint
Mapped type properties must be separated properly.
✗ Incorrect
The code misses a semicolon between two mapped properties, causing a syntax error.
📝 Syntax
advanced2:00remaining
Which mapped type produces keys with prefix and suffix using template literals?
Choose the mapped type that creates keys like 'pre_one_suf' and 'pre_two_suf' from union 'one' | 'two'.
Typescript
type Keys = 'one' | 'two';
Attempts:
2 left
💡 Hint
Use 'as' to rename keys with template literals.
✗ Incorrect
Option A correctly uses 'as' to rename keys with template literals. Others misuse syntax or types.
🚀 Application
expert2:00remaining
Count keys in a mapped type with template literals
Given this mapped type, how many keys does
Config have?Typescript
type Modes = 'auto' | 'manual' | 'off'; type Config = { [M in Modes as `${M}Mode`]: number; };
Attempts:
2 left
💡 Hint
Each union member creates one key with 'Mode' suffix.
✗ Incorrect
The union has 3 members, each mapped to one key with suffix 'Mode', so total keys are 3.