Closed
Description
// @strictNullChecks: true
function f(x: { [key: string]: number; } | null | undefined) {
for (const key in x) { // 1
console.log(x[key]); // 2
}
}
Expected behavior:
The for..in
statement in ECMAScript does not throw if the expression is undefined
or null
, but rather the loop body is not evaluated. As such, the correct behavior under --strictNullChecks
should be:
- The
for..in
statement (1) should be legal, even ifx
isundefined
ornull
. - Inside the
for..in
statement, thex
(2) should no longer be consideredundefined
ornull
.
Actual behavior:
- The
for..in
statement (1) reports the error:Object is possibly 'null' or 'undefined'.
- Inside the
for..in
statement, thex
(2) reports the error:Object is possibly 'null' or 'undefined'.