admin管理员组文章数量:1188236
I want to define an interface for tree structure.
Each node can have zero or more children:
export interface TreeNode {
children?: Array<TreeNode>;
}
I have implemented a traversing function for TreeNode
s.
export function traverseTree(treeData: Array<TreeNode> | TreeNode, callback: (treeNode: any) => any) {
// implementation omitted
}
I want to test it. Code is as follows:
const treeData = [
{
name: "root_1",
children: [
{
name: "child_1",
children: [
{
name: "grandchild_1"
},
{
name: "grandchild_2"
}
]
}
]
},
{
name: "root_2",
children: []
}
];
const traversingHistory = [];
const callback = (treeNode: any) => {
traversingHistory.push(treeNode.name);
}
traverseTree(treeData, callback);
However, compilation fails because treeData
's argument of type cannot be applied to traverseTree
.
I don't want to add attribute name
to interface TreeNode
because a tree node can have dynamic properties. How can I modify TreeNode
interface to accept more general types?
I want to define an interface for tree structure.
Each node can have zero or more children:
export interface TreeNode {
children?: Array<TreeNode>;
}
I have implemented a traversing function for TreeNode
s.
export function traverseTree(treeData: Array<TreeNode> | TreeNode, callback: (treeNode: any) => any) {
// implementation omitted
}
I want to test it. Code is as follows:
const treeData = [
{
name: "root_1",
children: [
{
name: "child_1",
children: [
{
name: "grandchild_1"
},
{
name: "grandchild_2"
}
]
}
]
},
{
name: "root_2",
children: []
}
];
const traversingHistory = [];
const callback = (treeNode: any) => {
traversingHistory.push(treeNode.name);
}
traverseTree(treeData, callback);
However, compilation fails because treeData
's argument of type cannot be applied to traverseTree
.
I don't want to add attribute name
to interface TreeNode
because a tree node can have dynamic properties. How can I modify TreeNode
interface to accept more general types?
3 Answers
Reset to default 14The error you are probably getting is:
Object literal may only specify known properties, and 'name' does not exist in type
If you want to allow other keys, it's got to be part of the type. You can do this with an index signature:
export interface TreeNode {
[key: string]: any // type for unknown keys.
children?: TreeNode[] // type for a known property.
}
I guess that using a generic
instead [key: string]: any
fits better:
export type Tree<T> = T & {
children?: T[];
}
keeping the required and optional fields of the Item
type we want to build a tree: Tree<Item>
for this question, the Item is: { name: string }
.
Try using this union type with object
:
type TreeNode = {
children?: Array<TreeNode>;
} & object;
本文标签: javascriptTypescript interface for tree structureStack Overflow
版权声明:本文标题:javascript - Typescript interface for tree structure - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738368126a2081985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论