Grokking TypeScript
0% completed
Recursive Types
A folder can contain other folders, and each of those can contain more folders. A reply to a comment can have its own replies in the same way. To describe data like this, a type must refer to itself, and such a type is called a recursive type.
Here is a comment thread, where each reply is another comment.
type ThreadComment = { text: string; replies?: ThreadComment[]; };
The replies property uses the type that is being defined. The compiler accepts this because it does not need to expand the type completely. It looks at the inner `ThreadComment
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%