【发布时间】:2022-01-20 06:25:26
【问题描述】:
我有一个递归接口,如下所示:
interface TreeNode {
id: string
children: TreeNode[]
}
我想像这样扩展TreeNode:
interface TreeNodeWithMorePros extends TreeNode{
moreProps: any[]
}
但这还不够,因为root 可以拥有moreProps,但孩子们不能。
const tree: TreeNodeWithMorePros = {
id: 'root',
moreProps: [],
children: [
{
id: 'child',
children: [],
// here I cannot use moreProps
}
]
}
如何正确扩展TreeNode 接口?
【问题讨论】:
标签: typescript types interface typescript-typings typescript-generics