A mapped type creates a new type by transforming each property of an existing type. It uses syntax like { [K in keyof T]: ... } to loop over keys.
Click to reveal answer
beginner
What does a conditional type do in TypeScript?
A conditional type chooses one type or another based on a condition, using syntax like T extends U ? X : Y. It works like an if-else for types.
Click to reveal answer
intermediate
How do mapped types and conditional types work together?
You can use conditional types inside mapped types to change each property type based on a condition. For example, making some properties optional or changing their type.
Click to reveal answer
intermediate
Example: What does this mapped type do?
type ReadonlyIfString<T> = { [K in keyof T]: T[K] extends string ? Readonly : T[K] }
It makes properties readonly only if their type is string. Other properties keep their original type.
Click to reveal answer
beginner
Why use mapped types with conditional types?
They let you create flexible types that adapt property types based on conditions, making your code safer and easier to maintain.
Click to reveal answer
What does [K in keyof T] mean in a mapped type?
ADelete keys from T
BLoop over all keys of type T
CCreate a new key K unrelated to T
DAssign a fixed key K
✗ Incorrect
It loops over each key K in the keys of type T to create a new type.
In T extends U ? X : Y, what happens if T is assignable to U?
AType becomes X
BType becomes Y
CType becomes T
DType becomes U
✗ Incorrect
If T fits U, the conditional type resolves to X.
Which is a valid use of conditional types inside a mapped type?
AAdding new properties
BChanging property names
CChanging property types based on their original type
DRemoving keys from the original type
✗ Incorrect
Conditional types can change property types but not keys or add/remove properties.
What does this type do?
type OptionalIfNumber<T> = { [K in keyof T]: T[K] extends number ? T[K] | undefined : T[K] }
AMakes number properties readonly
BMakes all properties optional
CRemoves number properties
DMakes number properties optional
✗ Incorrect
It makes properties optional only if their type is number.
Why are mapped types with conditional types useful?
AThey create flexible, type-safe transformations of object types
BThey speed up runtime code
CThey replace functions
DThey allow dynamic property names
✗ Incorrect
They help create new types by transforming existing ones safely and flexibly.
Explain how mapped types and conditional types can be combined to change property types based on their original types.
Think about looping over keys and using if-else logic for types.
You got /3 concepts.
Describe a real-life scenario where using a mapped type with conditional types would make your TypeScript code safer or easier to maintain.
Consider objects with mixed property types needing different rules.
You got /3 concepts.
Practice
(1/5)
1. What does a mapped type with conditional types do in TypeScript? type Example<T> = { [K in keyof T]: T[K] extends string ? number : boolean }
easy
A. It creates a new type where string properties become number and others become boolean.
B. It creates a new type with all properties removed.
C. It changes all properties to string type.
D. It only keeps properties that are strings.
Solution
Step 1: Understand mapped types
Mapped types loop over each key in the original type to create a new type with transformed properties.
Step 2: Apply conditional types
The conditional type checks if the property type extends string; if yes, it changes to number, else to boolean.
Final Answer:
It creates a new type where string properties become number and others become boolean. -> Option A
Quick Check:
Mapped + conditional changes types based on condition [OK]
Thinking it removes properties instead of transforming them
Assuming all types become string
Confusing conditional type logic
2. Which of the following is the correct syntax for a mapped type with a conditional type in TypeScript?
easy
A. type NewType<T> = { [K in T]: T[K] extends number ? string : boolean }
B. type NewType<T> = { [K in keyof T]: T[K] extends number ? string : boolean }
C. type NewType<T> = { [K of keyof T]: T[K] extends number ? string : boolean }
D. type NewType<T> = { K in keyof T: T[K] ? string : boolean }
Solution
Step 1: Check mapped type syntax
The correct syntax uses square brackets with 'in' keyword: [K in keyof T].
Step 2: Verify conditional type usage
The conditional type is written as T[K] extends number ? string : boolean, which is correct.
Final Answer:
type NewType<T> = { [K in keyof T]: T[K] extends number ? string : boolean } -> Option B
Quick Check:
Mapped type syntax = [K in keyof T] [OK]
Hint: Mapped types use [K in keyof T], not 'of' or missing brackets [OK]
Common Mistakes:
Using 'K in T' instead of 'K in keyof T'
Using 'K of keyof T' which is invalid
Missing square brackets around K in mapped type
3. Given the code:
type Flags = { a: string; b: number; c: boolean };
type Result = { [K in keyof Flags]: Flags[K] extends number ? 'num' : 'not-num' };
const test: Result = { a: 'not-num', b: 'num', c: 'not-num' };
What is the type of test.b?
medium
A. 'not-num'
B. number
C. 'num'
D. boolean
Solution
Step 1: Analyze the mapped type Result
For each key in Flags, if the property type is number, Result's property is 'num', else 'not-num'.
Step 2: Check property b in Flags
b is number, so Result['b'] is 'num'.
Final Answer:
'num' -> Option C
Quick Check:
Flags['b'] is number, so Result['b'] = 'num' [OK]
Hint: Check original type, then apply conditional mapping [OK]
Common Mistakes:
Confusing original property type with mapped type
Assuming test.b is number instead of 'num'
Ignoring conditional type logic
4. Identify the error in this mapped type:
type Fix<T> = { [P in keyof T]: T[P] extends string ? number : }
medium
A. Incorrect use of keyof operator
B. Cannot use conditional types inside mapped types
C. Square brackets missing around P
D. Missing type after colon in conditional type
Solution
Step 1: Check conditional type syntax
Conditional types require both true and false branches separated by colon.
Step 2: Identify missing false branch
The code ends with ':' but no type after it, causing syntax error.
Final Answer:
Missing type after colon in conditional type -> Option D
Quick Check:
Conditional types need both branches [OK]
Hint: Conditional types need both true and false parts [OK]
Common Mistakes:
Leaving out false branch in conditional type
Misplacing square brackets in mapped type
Thinking keyof is wrong here
5. How can you create a mapped type that keeps only the keys of an object whose values are assignable to a specific type, for example, only keys with string values? Choose the correct mapped type:
hard
A. type FilterStrings<T> = { [K in keyof T as T[K] extends string ? K : never]: T[K] }
B. type FilterStrings<T> = { [K in keyof T]: T[K] extends string ? never : K }
C. type FilterStrings<T> = { [K in keyof T]: T[K] extends string ? string : never }
D. type FilterStrings<T> = { [K in keyof T]: K extends string ? T[K] : never }
Solution
Step 1: Understand key remapping in mapped types
Using 'as' allows filtering keys by condition, replacing keys with 'never' to omit them.
Step 2: Check each option
type FilterStrings<T> = { [K in keyof T as T[K] extends string ? K : never]: T[K] } correctly uses 'as' with conditional to keep keys where T[K] extends string; others become never (excluded).
Final Answer:
type FilterStrings<T> = { [K in keyof T as T[K] extends string ? K : never]: T[K] } -> Option A
Quick Check:
Key remapping with 'as' filters keys [OK]
Hint: Use 'as' with conditional to filter keys in mapped types [OK]
Common Mistakes:
Not using 'as' for key filtering
Returning never as value instead of key
Confusing key and value conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.