javascript - 用于嵌套对象数组的 Typescript 接口
问题描述
我是 Typescript 的新手,我必须为以下道具类型构建一个界面:
const myProps = [
{
name: 'name1',
on: true,
children: [
{ name: 'test1', href: '#' },
{ name: 'test2', href: '#' },
{ name: 'test3', href: '#' }
]
},
{
name: 'name2',
on: false,
children: [
{ name: 'test1', href: '#' },
{ name: 'test2', href: '#' },
{ name: 'test3', href: '#' },
{ name: 'test4', href: '#' },
{ name: 'test5', href: '#' }
]
},
{
name: 'name3',
on: false,
children: [{ name: 'test1', href: '#' }]
}
];
我想为它创建一个界面,以便在 React + Typescript 应用程序中使用。
这是目前的界面:
export interface IChildren {
name: string,
href: string
}
export interface IMyProps {
name: string,
on: boolean,
children: IChildren,
}
它不起作用,我猜它应该有一些数组。有什么建议么?
解决方案
你可以这样试试
export interface CommonProps {
name: string;
href: string;
}
export interface MyProps {
name: string;
on: boolean;
children: Array<CommonProps>;
}
另请注意,数据接口不应以命名约定开头 具有方法声明的
"I"
接口应具有"I"
IMethodService
推荐阅读
- php - Data not transferred between 2 forms using php-echo
- c++ - 内存泄漏到 v8 共享库 (dll) 版本 4.1.0.3
- c - MPI Gatherv 不工作(消息被截断)
- asp.net - IdentityServer4 与 ASP.Net 身份项目架构
- javascript - 将 URL 中的 HTML 内容插入 HTML(表单、div、iframe、...)
- tsql - 同一表上的 T-SQL 2 个或更多联接
- python - Django,form.save(),不在管理页面上保存
- postgresql - 无法在 Ubuntu 上安装 postgres (E: Unable to locate package postgresql)
- azure - 保护 Azure 函数
- angular - 角度双向绑定不适用于数据输入