NestedArray<number> when used as NestedArray<number> = [1, [2, [3]]]?type NestedArray<T> = T | NestedArray<T>[]; const example: NestedArray<number> = [1, [2, [3]]];
The type NestedArray<T> is defined recursively as either a value of type T or an array of NestedArray<T>. This means it can be a number, or an array of numbers, or an array of arrays of numbers, and so on, to any depth.
Tree<string>?type Tree<T> = {
value: T;
children?: Tree<T>[];
};
const example: Tree<string> = {
value: "root",
children: [
{ value: "child1" },
{ value: "child2", children: [{ value: "grandchild" }] }
]
};The Tree<T> type defines an object with a value of type T and an optional children property which is an array of Tree<T> objects. This allows for recursive tree structures.
type Recursive<T> = T | Recursive<T>[];
Option D uses Recursive[] without specifying the generic parameter <T>. This causes a syntax error because Recursive is a generic type and must be used with its parameter.
DeepReadonly<{ a: { b: number } }> given the definition below?type DeepReadonly<T> = {
readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P];
};
type Example = DeepReadonly<{ a: { b: number } }>;
// What is the type of Example?The DeepReadonly type recursively marks all properties as readonly. For nested objects, it applies itself again, so a and b are both readonly.
T?type RecursiveConstraint<T extends { children?: T[] }> = T;The constraint T extends { children?: T[] } means that T must be an object type that may have a children property which is an array of T itself, enabling recursive structures.