Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rbuckton opened this issue Oct 25, 2017 · 0 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@rbuckton
Copy link
Member

rbuckton commented Oct 25, 2017

// @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'.
@rbuckton rbuckton 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 Oct 25, 2017
@rbuckton rbuckton 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 Oct 25, 2017
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 25, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Oct 25, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@mhegazy mhegazy assigned weswigham and unassigned sandersn Apr 25, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, TypeScript 3.1 Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants