0
0
Typescriptprogramming~20 mins

Basic mapped type syntax 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 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?
A{ [key: string]: number }
B{ a: string; b: string; c: string; }
C{ a: number; b: number; c: number; }
D{ a?: number; b?: number; c?: number; }
Attempts:
2 left
💡 Hint
Mapped types create a new type by looping over keys and assigning the given type.
Predict Output
intermediate
2: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?
A{ x?: boolean; y?: boolean; }
B{ readonly x: boolean; readonly y: boolean; }
C{ readonly [key: string]: boolean }
D{ x: boolean; y: boolean; }
Attempts:
2 left
💡 Hint
The readonly modifier makes all properties immutable.
Predict Output
advanced
2: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?
A{ id: number; name: number; age: number; }
B{ id?: number; name?: string; age?: string; }
C{ id: string; name: string; age: string; }
D{ id: number; name: string; age: string; }
Attempts:
2 left
💡 Hint
Conditional types can assign different types based on the key.
Predict Output
advanced
2: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?
A{ prefix_a: number; prefix_b: number; }
B{ a: number; b: number; }
C{ prefix_a?: number; prefix_b?: number; }
D{ [key: string]: number }
Attempts:
2 left
💡 Hint
Key remapping lets you change property names in mapped types.
Predict Output
expert
3: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?
A{ readonly _one: string; readonly _two: number; }
B{ _one?: string; _two?: number; }
C{ _one: string; _two: number; }
D{ readonly _one?: string; readonly _two?: number; }
Attempts:
2 left
💡 Hint
Modifiers + and - control readonly and optional flags; key remapping changes keys.