I have a class defined like this
class Foo {
value: string | null;
constructor(){
this.value = null;
}
private ensureValueExists():this.value is string{ //type predicate is not legal
this.value = "bar";
return true;
}
doStuffWithValue(){
this.ensureValueExists();
return 5 + this.value; //ERROR, this.value can be null
}
}
I would like for the ensureValueExists method to be able to tell to the compiler that this.value is indeed a string and safe to use. Is there a special syntax to use or is it no doable with TS at the moment for methods ?