Skip to content

'for..in' for null/undefined expression should not be an error in --strictNullChecks #19461

Closed

Description

@rbuckton
// @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 if x is undefined or null.
  • Inside the for..in statement, the x (2) should no longer be considered undefined or null.

Actual behavior:

  • The for..in statement (1) reports the error: Object is possibly 'null' or 'undefined'.
  • Inside the for..in statement, the x (2) reports the error: Object is possibly 'null' or 'undefined'.

Activity

  1. changed the title [-]'for..in' for 'undefined' expression should not be an error in '--strictNullChecks'[/-] [+]'for..in' for undefined expression should not be an error in --strictNullChecks[/+] on Oct 25, 2017
  2. changed the title [-]'for..in' for undefined expression should not be an error in --strictNullChecks[/-] [+]'for..in' for null/undefined expression should not be an error in --strictNullChecks[/+] on Oct 25, 2017
  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions