microsoft / microsoft/TypeScript

Diagnostic about "left-hand side of a 'for...in' statement" type is out of date

Open
#13,655 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.1.5

Now that the left side of a 'for...in' statement is apparently allowed to be keyof the right side, the diagnostic for when the left side has the wrong type needs to be updated to account for that possibility.

(Aside: Is it intentional that the left side won't be inferred to have the keyof type because of the unsoundness described in #12314, but it is allowed to be explicitly declared as that type?)

Code

interface Foo {
  x: number;
  y: string;
}

function scan(f: Foo) {
  let k: number;
  for (k in f) {  // Error: "The left-hand side of a 'for...in' statement must be of type 'string' or 'any'."
    console.log(k);
  }
  let k2: keyof Foo;
  for (k2 in f) {  // No error
    console.log(k2);
  }
}

Expected behavior:
Something like "The index type of the right side of a 'for...in' statement must be assignable to the left side" perhaps, with details on the failure of assignability?

Actual behavior:
"The left-hand side of a 'for...in' statement must be of type 'string' or 'any'."

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided for...in example and comparing the diagnostic for number with the accepted keyof Foo case. The issue names no source file or test; locate the type-checker diagnostic and its existing tests, then update the wording and verify that the incorrect and valid cases are covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.