microsoft / microsoft/TypeScript

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

未关闭
#13,655 6 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Experience Enhancement Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先复现提供的 for...in 示例,并将 number 的诊断与被接受的 keyof Foo 情况进行比较。该 issue 未指出源文件或测试;定位类型检查器的诊断及其现有测试,然后更新措辞并验证错误和有效情况都已覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。