Trung BìnhTypeScript iconTypeScript

Recursive types trong TypeScript là gì?

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.

Xem toàn bộ TypeScript cùng filter theo level & chủ đề con.

Mở danh sách TypeScript