'How type recursive object in typescript?
I have next object
const obj = {
name: 'First',
age: 22,
}
This object has the next interface
interface ITask {
name: string,
age: number
}
but after some data mapping I create a recursive object like this
const obj = {
name: 'First',
age: 22,
next: {
name: 'Second',
age: 12,
next: { EMPTY OBJECT WHEN END }
}
}
I try type this object this way, but it doesnt work
type IRecursiveTask = {
[key: string]: IRecursiveTask
} & ITask
Solution 1:[1]
type defines a type aliases, and type aliases cannot reference themselves. However, interface can.
interface IRecursiveTask {
name: string;
age: number;
next: IRecursiveTask | {};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Silvio Mayolo |
