obj after applying the DeepReadonly mapped type to data?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?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.
DeepPartial type below, what is the type of partialUser?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"
}
}
};The DeepPartial type makes all properties optional recursively, so partialUser can have any subset of properties at any depth.
DeepRequired type is intended to make all properties required recursively. Which option correctly identifies the error in the code?type DeepRequired<T> = {
[P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
};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.
Option A correctly places readonly before the property and uses a conditional type to recurse only on objects.
DeepStringToNumber that recursively changes all string properties to number, leaving other types unchanged?Option D correctly checks if the property is a string first, then if it is an object to recurse, otherwise leaves it unchanged.
