简体   繁体   中英

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

type defines a type aliases, and type aliases cannot reference themselves. However, interface can.

interface IRecursiveTask {
  name: string;
  age: number;
  next: IRecursiveTask | {};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM