Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript错误对象可能为“未定义”_Typescript - Fatal编程技术网

Typescript错误对象可能为“未定义”

Typescript错误对象可能为“未定义”,typescript,Typescript,我需要有关修复代码中错误的帮助: export class BSPTree { leaf: Box; lchild: undefined; rchild: undefined; constructor(leaf: Box){ this.leaf = leaf; } getLeafs(){ if (this.lchild === undefined && this.rchild === undef

我需要有关修复代码中错误的帮助:

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())
    }

为什么我会犯这个错误?

您需要定义lchild和rchild的类型,并在它们未定义时解决它们的问题

导出类树{ 叶子:盒子;
lchild:undefined | BSPTree;//因为如果lchild或rchild中的一个是未定义的demorganh,则可以满足else条件。如何解决此问题?您可以处理其中一个未定义的情况。我不知道您的业务逻辑,但只要将第一个条件&&更改为| |,以便在其中一个子项未定义时返回叶就可以了已定义,但具体取决于您。请注意,这将导致数组中的未定义,因为选项的计算结果为未定义…我不认为这正是此处需要的。这是如何解决它的示例,可能作者没有使用TS 3.7。我添加了| |[]