我需要代码修复错误的帮助:
export class BSPTree {
leaf: Box;
lchild: undefined;
rchild: undefined;
constructor(leaf: Box){
this.leaf = leaf;
}
getLeafs(){
if (this.lchild === undefined && this.rchild === undefined)
return [this.leaf]
else
return [].concat(this.lchild.getLeafs(), this.rchild.getLeafs())
}
为什么会出现此错误?
答案 0 :(得分:0)
您需要定义lchild
和rchild
的类型,并在undefined
时解决它们的情况。
export class BSPTree {
leaf: Box;
lchild: undefined | BSPTree; // <- here
rchild: undefined | BSPTree; // <- here
constructor(leaf: Box){
this.leaf = leaf;
}
getLeafs(){
if (this.lchild === undefined && this.rchild === undefined)
return [this.leaf]
else
return [].concat(this.lchild?.getLeafs() || [], this.rchild?.getLeafs() || []) // <- here ?
}