Challenge - 5 Problems
Deep Mapped Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a deep readonly mapped type
What is the type of
obj after applying the DeepReadonly mapped type to data?Typescript
type DeepReadonly<T> = {
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K]
};
const data = {
name: "Alice",
details: {
age: 30,
hobbies: ["reading", "hiking"]
}
};
const obj: DeepReadonly<typeof data> = data;
// What happens if we try to assign obj.details.age = 31?Attempts:
2 left
💡 Hint
Think about what 'readonly' means in TypeScript and how it applies recursively.
✗ Incorrect
The DeepReadonly type recursively marks all properties as readonly, including nested objects. So trying to assign a new value to obj.details.age causes a TypeScript compile-time error.
❓ Predict Output
intermediate2:00remaining
Output of a deep partial mapped type
Given the
DeepPartial type below, what is the type of partialUser?Typescript
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
};
interface User {
id: number;
profile: {
name: string;
address: {
city: string;
zip: string;
};
};
}
const partialUser: DeepPartial<User> = {
profile: {
address: {
city: "New York"
}
}
};Attempts:
2 left
💡 Hint
Look at how the optional modifier and recursion are applied in the mapped type.
✗ Incorrect
The DeepPartial type makes all properties optional recursively, so partialUser can have any subset of properties at any depth.
🔧 Debug
advanced2:00remaining
Identify the error in a deep required mapped type
The following
DeepRequired type is intended to make all properties required recursively. Which option correctly identifies the error in the code?Typescript
type DeepRequired<T> = {
[P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
};Attempts:
2 left
💡 Hint
Consider what types extend object in TypeScript and how that affects recursion.
✗ Incorrect
Since arrays and functions are also objects, the recursive condition causes infinite recursion or incorrect behavior. A better approach is to exclude arrays and functions from recursion.
📝 Syntax
advanced2:00remaining
Which mapped type syntax is correct for deep transformation?
Which option correctly defines a mapped type that recursively makes all properties readonly?
Attempts:
2 left
💡 Hint
Remember to check the placement of 'readonly' and the conditional type.
✗ Incorrect
Option A correctly places readonly before the property and uses a conditional type to recurse only on objects.
🚀 Application
expert3:00remaining
Create a mapped type to deeply transform all string properties to number
Which option defines a mapped type
DeepStringToNumber that recursively changes all string properties to number, leaving other types unchanged?Attempts:
2 left
💡 Hint
Check the order of conditional checks to handle strings before objects or vice versa.
✗ Incorrect
Option D correctly checks if the property is a string first, then if it is an object to recurse, otherwise leaves it unchanged.