I am encountering an issue with a property that can be null in my code. Even though I check for the value not being null and being an array before adding a new value to it, the type checker still considers the value as potentially null. Can anyone shed light on why this is happening and how I can resolve it? Thanks in advance.
class BIT {
head: BIT | null = this;
private value: string;
right: BIT | BIT[] | null = null;
left: BIT | BIT[] | null = null;
somefunction(node: BIT) {
if (this.left !== null) {
if (this.left instanceof Array) {
this.left.push(node);
}
}
}
}
UPDATE: I noticed that by directly including the conditions in the if statement instead of using boolean variables like haveLeft and leftIsBranch, everything works properly. Can someone explain what is causing this behavior?