When I initialize a variable to null, but with another type as a union (e.g. number | null), it seems to lose the non-null type information immediately (the same thing happens with undefined, except if I leave it unitialized, rather than explicitly setting it to undefined).
let foo: number | null = null;
foo; //foo's type (seen by mousing over) is "null", not "number | null"
function blah() {
foo = 123; //foo is "number | null"
}
blah();
foo; //foo's type is "null", not "number | null"
TS Playground
Is there a way to access the type of foo correctly there?
Turns out this is a known trade off https://github.com/microsoft/TypeScript/issues/9998
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With