So I have the following code:
class A {
constructor(private num: number = 1) {
}
private changeNum() {
this.num = Math.random();
}
private fn() {
if(this.num == 1) {
this.changeNum();
if(this.num == 0.5) {
// do stuff
}
}
}
}
It is a class, which has a property num and a function changeNum that changes that number.
Then, it also has another function fn which firstly checks whether num is equal to 1 and if so, changes num through changeNum and checks it again.
The problem is that Typescript does not detect that num was changed through changeNum and throws the following error:
This condition will always return 'false' since the types '1' and '0.5' have no overlap.
Is there a way to get Typescript to recognize that num was changed through changeNum?
EDIT: One thing that I found is that changing if(this.num == 0.5) to if(this.num == Number(0.5)) makes the program compile correctly but that certainly doesn't seem like a good solution!