0
0
Typescriptprogramming~20 mins

Recursive generic types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive Generic Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a recursive generic type with nested arrays
What is the type of NestedArray<number> when used as NestedArray<number> = [1, [2, [3]]]?
Typescript
type NestedArray<T> = T | NestedArray<T>[];

const example: NestedArray<number> = [1, [2, [3]]];
A"example" must be an array of arrays of numbers with exactly 3 levels
B"example" can be a number or an array of numbers nested any depth
C"example" can only be a single number, not arrays
D"example" must be a flat array of numbers only
Attempts:
2 left
💡 Hint
Think about how the recursive type allows nesting arrays inside arrays.
Predict Output
intermediate
2:00remaining
Output of recursive generic type with object nodes
Given the type below, what is the type of Tree<string>?
Typescript
type Tree<T> = {
  value: T;
  children?: Tree<T>[];
};

const example: Tree<string> = {
  value: "root",
  children: [
    { value: "child1" },
    { value: "child2", children: [{ value: "grandchild" }] }
  ]
};
A"example" is an object with a value of type string and optional children which are arrays of similar objects recursively
B"example" is a flat object with only a value string and no children allowed
C"example" must be an array of strings only
D"example" is a string or an array of strings
Attempts:
2 left
💡 Hint
Look at how the children property is defined recursively.
🔧 Debug
advanced
2:00remaining
Identify the error in recursive generic type definition
Which option contains a syntax error in the recursive generic type definition?
Typescript
type Recursive<T> = T | Recursive<T>[];
Atype Recursive<T> = T | Recursive<T>[];
Btype Recursive<T> = T | Recursive<T>;
Ctype Recursive<T> = T | Recursive<T>[] | null;
Dtype Recursive<T> = T | Recursive[];
Attempts:
2 left
💡 Hint
Check if the generic parameter is correctly used in the recursive reference.
Predict Output
advanced
2:00remaining
Output of a mapped recursive generic type
What is the resulting type of DeepReadonly<{ a: { b: number } }> given the definition below?
Typescript
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?
A{ readonly a: { readonly b: number } }
B{ a: { b: number } }
C{ readonly a: { b: number } }
D{ a: { readonly b: number } }
Attempts:
2 left
💡 Hint
The type recursively applies readonly to all nested properties.
🧠 Conceptual
expert
3:00remaining
Understanding recursive generic constraints
Consider the recursive generic type below. What constraint does it enforce on the type parameter T?
Typescript
type RecursiveConstraint<T extends { children?: T[] }> = T;
AT must be an array type
BT can be any type including primitives
CT must be an object type that optionally has a children property which is an array of T
DT must be a function type with children as parameters
Attempts:
2 left
💡 Hint
Look at the constraint after the extends keyword carefully.