TypeScript hỗ trợ recursive type aliases.
Hữu ích cho tree structures, nested JSON, linked lists.
typescript
type TreeNode<T> = {
value: T;
children: TreeNode<T>[]; // tự tham chiếu
};
// JSON có thể dùng JSON type chuẩn:
type Json = string | number | boolean | null
| Json[] | { [key: string]: Json };Interface luôn hỗ trợ recursive (vì là named type), type alias cũng hỗ trợ từ TS 3.7.
TypeScript supports recursive type aliases.
Useful for tree structures, nested JSON, and linked lists.
typescript
type TreeNode<T> = {
value: T;
children: TreeNode<T>[]; // self-reference
};
// JSON type example:
type Json = string | number | boolean | null
| Json[] | { [key: string]: Json };Interfaces have always supported recursion (as named types); type aliases also support it since TS 3.7.