如何安全地遍历对象的键并通过打字稿中的键打印所有值?

问题描述 投票:0回答:2

我正在学习打字稿,并且试图定义一个遍历对象值的函数,但是当我这样做时,tslint告诉我:

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

这是我的代码:

function proxyData(data: object) {
  Object.keys(data).forEach((key: string) => {
    console.log(data[key]);
  });
}
typescript tslint
2个回答
0
投票

在打字稿对象中,默认情况下没有定义任何键。如果要访问已知结构上的键,请明确指定数据对象。


0
投票

按定义定义对象,

© www.soinside.com 2019 - 2024. All rights reserved.