microsoft / microsoft/TypeScript

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

オープン
#13,655 コメント 6 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Experience Enhancement Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供されている for...in の例を再現し、number に対する診断を、受け入れられるケースである keyof Foo と比較します。この issue にはソースファイルもテストも記載されていないため、type-checker の診断と既存のテストを見つけ、文言を更新して、誤ったケースと有効なケースがカバーされていることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。