Lesson 16 of 56 · Typescript
Recursive Types
Duration: 15 minutes
Recursive Type Definitions
Used for hierarchical data like trees or linked structures.
type TreeNode<T> = {
value: T;
children?: TreeNode<T>[];
};
const root: TreeNode<string> = {
value: "root",
children: [{ value: "child" }]
};
JSON Recursive Types
When encoding/decoding JSON, recursive types help keep type safety.
interface Comment {
id: number;
text: string;
replies?: Comment[]; // recursive reference
}