0
0
Typescriptprogramming~20 mins

Mapped type with template literals in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mapped Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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?
A{ successMessage: string; error: string; }
B{ success: string; error: string; }
C{ success: string; errorMessage: string; }
D{ successMessage: string; errorMessage: string; }
Attempts:
2 left
💡 Hint
Template literals combine each union member with 'Message' suffix.
Predict Output
intermediate
2: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;
  };
};
A{ smallSize: { red: boolean; blue: boolean; }; mediumSize: { red: boolean; blue: boolean; }; }
B{ small: { red: boolean; blue: boolean; }; medium: { red: boolean; blue: boolean; }; }
C{ smallSize: { redColor: boolean; blueColor: boolean; }; mediumSize: { redColor: boolean; blueColor: boolean; }; }
D{ small: { redColor: boolean; blueColor: boolean; }; medium: { redColor: boolean; blueColor: boolean; }; }
Attempts:
2 left
💡 Hint
Both outer and inner keys use template literals with 'Size' and 'Color' suffixes.
🔧 Debug
advanced
2: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;
};
ASyntaxError: Missing semicolon between mapped properties
BSyntaxError: Unexpected token '['
CTypeError: Cannot have multiple mapped signatures in one type
DSyntaxError: Duplicate identifier 'K' in mapped type
Attempts:
2 left
💡 Hint
Mapped type properties must be separated properly.
📝 Syntax
advanced
2: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';
A{ [K in Keys as `pre_${K}_suf`]: boolean }
B{ [K in `pre_${Keys}_suf`]: boolean }
C{ [K in Keys]: boolean as `pre_${K}_suf` }
D{ [K in Keys]: boolean & `pre_${K}_suf` }
Attempts:
2 left
💡 Hint
Use 'as' to rename keys with template literals.
🚀 Application
expert
2: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;
};
A6
B3
C1
D0
Attempts:
2 left
💡 Hint
Each union member creates one key with 'Mode' suffix.