Closed
Description
TypeScript Version: 2.0.2 (RC)
Code
function test(lines: string[]) {
var curThing: { x: number } | null = null;
lines.forEach(line => {
curThing = { x: 1 };
});
if (curThing) {
console.info(curThing.x);
}
}
Expected behavior:
No errors.
Actual behavior:
When strict null checks are on, TS2 RC deduces that curThing
in the if
at the end is of type null
and inside the block is of type never
. The error I get then is that never
has no member named x. Fair enough.
In this case I can sidestep the issue by using for (var line of lines) { … }
instead of the forEach method, but that may not always be the case. BTW, this is a reduced case from some parser code.
This case also deals with T | null
but this applies to any T | U
type value. I tested this separately with a string | number
initialised to a number and then set to a string, etc.