type incorrectly narrowed because assignment of a different value in function not seen
Closed this issue ยท 2 comments
190n commented
๐ Search Terms
"incorrect narrowing," "control flow assignment"
๐ Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about incorrect narrowing
โฏ Playground Link
๐ป Code
let foo: { bar: number } | null = null;
function assign() {
foo = { bar: 5 };
}
while (true) {
if (foo === null) {
assign();
} else {
void foo.bar;
}
}๐ Actual behavior
There is an error on the bar property lookup. foo has been narrowed to null because TypeScript thinks we never assign it to something other than null, and in the else branch it is narrowed to never because we've checked it is not null so there are no more types it could possibly be.
๐ Expected behavior
There should be no error. If TypeScript cannot prove that foo is never assigned a non-null value then it should not assume that.
Additional information about the issue
No response
190n commented
Ah sorry I didn't find that one, thanks.