Typescript 无法对具有两种潜在类型(Biginteger | null)的类属性使用typeof
我有一个类,其公共属性可以是biginteger或null,如下所示。我希望能够使用typeof检测它是否为null: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
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){}