Typescript 在TS[TS(7053)]

Typescript 在TS[TS(7053)],typescript,Typescript,让我们看一下简单的代码: let obj = { a:1, b:2, c:3 } for (let i in obj) { console.log(obj[i]); } 我们在obj[i]上得到一个错误: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ a: number; b: number; c: number;

让我们看一下简单的代码:

let obj = {
  a:1,
  b:2,
  c:3
}
for (let i in obj) {
  console.log(obj[i]);
}
我们在obj[i]上得到一个错误:

Element implicitly has an 'any' type because expression of type 'string' 
can't be used to index type '{ a: number; b: number; c: number; }'.
  No index signature with a parameter of type 'string' was found 
on type '{ a: number; b: number; c: number; }'. ts(7053)

出什么问题了?

请参见@jcalz,但是
var i:keyof obj
会扼杀
让i
在for…in-scope of i内循环中每个迭代的目的,不是吗?是的。。。我想您可以像在
中那样为(让_iin obj){const I=_ias keyof typeof obj;console.log(obj[I])}
执行类型断言。这和其他任何方法一样不安全,因为缺乏。并解释了这背后的想法。