如何在 TypeScript 中迭代通用对象的键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43389414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:41:45  来源:igfitidea点击:

How to iterate over keys of a generic object in TypeScript?

typescriptecmascript-6

提问by Marcel

I need to iterate over a large object which is just typed as "object". It contains an unknown number of objects of the same type.

我需要迭代一个刚刚输入为“对象”的大对象。它包含未知数量的相同类型的对象。

In older posts I had found solutions using a generator within a custom Symbol.iterator function to make the large object iterable with a for..of loop.

在较早的帖子中,我找到了使用自定义 Symbol.iterator 函数中的生成器来使大对象可通过 for..of 循环迭代的解决方案。

But it seems to me, now in 2017, just using Object.keys is actually easier:

但在我看来,现在是 2017 年,只使用 Object.keys 实际上更容易:

Object.keys(bigObject).forEach((key:string)=>{
console.log(bigObject[key]);
});

This actually runs just fine! But the TypeScript compiler keeps giving me the error "error TS7017: Element implicitly h as an 'any' type because type '{}' has no index signature"

这实际上运行得很好!但是 TypeScript 编译器不断给我错误“错误 TS7017: 元素隐式为 'any' 类型,因为类型 '{}' 没有索引签名”

Does anybody have an idea what I am missing here? Or what is the current best practice to do such an iteration with ES2015 and TypeScript (2.2.2)?

有人知道我在这里缺少什么吗?或者使用 ES2015 和 TypeScript (2.2.2) 进行此类迭代的当前最佳实践是什么?

采纳答案by Paleo

It contains an unknown number of objects of the same type.

它包含未知数量的相同类型的对象。

Maybe with a genericinterface BigObject<T>for your dictionary?

也许你的字典有一个通用接口?BigObject<T>

interface BigObject<T> {
    [index: string]: T
}

let bigObject: BigObject<object> = {}
Object.keys(bigObject).forEach(key => {
  console.log(bigObject[key])
})

Here I wrote the type objectin let bigObject: BigObject<object>. You can use a better type.

在这里,我objectlet bigObject: BigObject<object>. 您可以使用更好的类型。

回答by Luke Machowski

for..in

对于..in

When looking at the Typescript documentation (Typescript: Iterators and Generators), we see that the for..in syntax will iterate over the keysof the object.

在查看 Typescript 文档(Typescript: Iterators and Generators)时,我们看到 for..in 语法将迭代对象的

for..in returns a list of keys on the object being iterated, whereas for..of returns a list of values of the numeric properties of the object being iterated.

for..in 返回被迭代对象的键列表,而 for..of 返回被迭代对象的数值属性的值列表。

We can use that to our advantage to index into our object and get the strongly typed value:

我们可以利用它来索引我们的对象并获得强类型值:

// Go through each key of the indexed object:
for (const key in indexedObject)
{
   // Get the indexed item by the key:
   const indexedItem = indexedObject[key];
   // Now we have the item.

   // Use it...
}

Solution

解决方案

We can use that to get an elegant solution to the question:

我们可以使用它来为这个问题提供一个优雅的解决方案:

// Go through each key in the bigObject:
for (const key in bigObject)
{
   // Get the strongly typed value with this name:
   const value = bigObject[key];
   // Now we have the the strongly typed value for this key (depending on how bigObject was typed in the first place).

   // Do something interesting with the property of bigObject...
}