microsoft / microsoft/TypeScript

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

Offen
#13,655 6 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experience Enhancement Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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'."

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bereitgestellte for...in-Beispiel zu reproduzieren, und vergleiche die Diagnose für number mit dem akzeptierten Fall keyof Foo. Das Issue nennt keine Quelldatei und keinen Test; finde die Diagnose des Typecheckers und die vorhandenen Tests, aktualisiere dann die Formulierung und verifiziere, dass die inkorrekten und gültigen Fälle abgedeckt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.