0
0
Typescriptprogramming~10 mins

Recursive generic types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Recursive generic types
Define generic type T
Type refers to itself with T
Type expands recursively
Base case stops recursion
Type fully resolved
A recursive generic type is a type that refers to itself with a generic parameter, expanding until a base case stops the recursion.
Execution Sample
Typescript
type NestedArray<T> = T | NestedArray<T>[];

const example: NestedArray<number> = [1, [2, [3, 4], 5], 6];
Defines a recursive generic type NestedArray that can be a value of type T or an array of NestedArray<T>.
Execution Table
StepActionType ExpressionEvaluationResulting Type
1Define NestedArray<T>T | NestedArray<T>[]Type refers to itselfRecursive type definition
2Instantiate NestedArray<number>number | NestedArray<number>[]Expands to number or array of NestedArray<number>Recursive expansion begins
3Evaluate example value[1, [2, [3, 4], 5], 6]Check each element matches NestedArray<number>Valid nested arrays and numbers
4Check base casenumberPrimitive type, recursion stops hereBase case reached
5Full type resolvedNestedArray<number>Nested arrays of numbers or numbersType accepted
💡 Recursion stops when the type reaches the base case (primitive type number).
Variable Tracker
VariableStartAfter Step 2After Step 3Final
TGeneric placeholdernumbernumbernumber
NestedArray<T>T | NestedArray<T>[]number | NestedArray<number>[]number | NestedArray<number>[]number | NestedArray<number>[]
exampleundefinedundefined[1, [2, [3, 4], 5], 6][1, [2, [3, 4], 5], 6]
Key Moments - 3 Insights
Why doesn't the recursive type cause infinite expansion?
Because the recursion stops at the base case when the type is the primitive type number, as shown in step 4 of the execution table.
How does TypeScript know when to stop expanding the recursive generic?
TypeScript stops expanding when it reaches a non-recursive type like number, which acts as the base case (step 4).
Can the recursive generic type hold both single values and arrays?
Yes, the type is defined as T or an array of NestedArray<T>, so it can hold single values or nested arrays, as shown in the example value in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the type of NestedArray<number> after step 2?
Anumber | NestedArray<number>[]
Bnumber[]
CNestedArray<number>
Dnumber
💡 Hint
Check the 'Resulting Type' column at step 2 in the execution table.
At which step does the recursion stop expanding the type?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the step where the base case 'number' is reached in the execution table.
If we changed T to string, how would the variable tracker change after step 2?
AT would be string[], NestedArray<T> would be string[] | NestedArray<string[]>[]
BT would be number, NestedArray<T> would be number | NestedArray<number>[]
CT would be string, NestedArray<T> would be string | NestedArray<string>[]
DNo change
💡 Hint
Refer to the variable tracker row for T and NestedArray after step 2.
Concept Snapshot
Recursive generic types allow a type to refer to itself with a generic parameter.
Syntax example: type NestedArray<T> = T | NestedArray<T>[];
This defines a type that can be a value of T or an array of itself.
Recursion stops at a base case type (like number or string).
Useful for nested data structures like trees or nested arrays.
Full Transcript
This visual execution trace shows how recursive generic types work in TypeScript. We start by defining a generic type NestedArray<T> that can be either a value of type T or an array of NestedArray<T>. When we instantiate it with number, the type expands recursively to allow nested arrays of numbers. The recursion stops when the type reaches the base case, which is the primitive type number. The example value demonstrates valid nested arrays and numbers matching the type. Variable tracking shows how the generic placeholder T is replaced by number and how the example variable holds the nested array structure. Key moments clarify why recursion stops and how the type can hold both single values and arrays. The quizzes test understanding of type expansion, recursion stopping point, and how changing T affects the type. The snapshot summarizes the concept with syntax and behavior. This helps beginners see step-by-step how recursive generic types expand and resolve.