Consider the code snippet below:
class A {
private foo : number = 3;
public method () {
if (this.foo === 3) {
this.anotherMethod();
if (this.foo === 4) {
console.log(this.foo);
}
}
}
public anotherMethod () {
this.foo = 4;
}
}
In this scenario, the value of foo
in the class is modified by the method anotherMethod
. This violates the assumption that the variable will always remain as "3". However, Typescript continues to display the error message:
error TS2367: This condition will always return 'false'
since the types '3' and '4' have no overlap.
How can I resolve this issue?