Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 无法对具有两种潜在类型(Biginteger | null)的类属性使用typeof_Typescript - Fatal编程技术网

Typescript 无法对具有两种潜在类型(Biginteger | null)的类属性使用typeof

Typescript 无法对具有两种潜在类型(Biginteger | null)的类属性使用typeof,typescript,Typescript,我有一个类,其公共属性可以是biginteger或null,如下所示。我希望能够使用typeof检测它是否为null: class Chart { public cx: BigInteger | null; public constructor() { this.cx = null; } } 如果我稍后检查以下内容: if ( typeof this.cx == "null") { console.l

我有一个类,其公共属性可以是biginteger或null,如下所示。我希望能够使用typeof检测它是否为null:

class Chart {

    public  cx: BigInteger | null;


    public constructor() {


        this.cx = null;


    }

}
如果我稍后检查以下内容:

  if ( typeof this.cx == "null") {
                console.log('is null')
            }
但这会产生错误:
由于类型“string”|“number”|“bigint”|“boolean”|“symbol”|“undefined”|“object”|“function”和“null”没有重叠,因此此条件将始终返回“false”。


如何检查此项

要检查
null
您不能使用
typeof
,只需使用
==
检查即可:

class Chart {
    public cx: BigInteger | null;

    public constructor() {
        this.cx = null;
    }

    public test() {
        if (this.cx === null) {
            console.log(this.cx);
        }
    }
}

.

要检查
null
您不能使用
typeof
,只需使用
==
检查即可:

class Chart {
    public cx: BigInteger | null;

    public constructor() {
        this.cx = null;
    }

    public test() {
        if (this.cx === null) {
            console.log(this.cx);
        }
    }
}

.

在JavaScript中,
typeof null==='object'
将解析为
true
,请参阅。您可以直接检查
null
如果(this.cx==null){}
在JavaScript中,
typeof null=='object'
将解析为
true
,请参阅。如果(this.cx==null){}