简体   繁体   English

如何在typescript中递归输入object?

[英]How type recursive object in typescript?

I have next object我有下一个 object

const obj = {
 name: 'First',
 age: 22,
}

This object has the next interface这个object有下一个界面

interface ITask {
 name: string,
 age: number
}

but after some data mapping I create a recursive object like this但经过一些数据映射后,我创建了一个递归的 object 这样的

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我尝试以这种方式输入 object,但它不起作用

type IRecursiveTask =  {
        [key: string]: IRecursiveTask
} & ITask

type defines a type aliases, and type aliases cannot reference themselves. type定义了类型别名,类型别名不能引用自己。 However, interface can.但是, interface可以。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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