for-in 语句 [英] for-in statement
问题描述
TypeScript 文档没有提到像 for
或 for-in
这样的循环.从使用语言来看,for
循环似乎只支持 any
或 string
变量.
TypeScript docs say nothing about loop like for
or for-in
. From playing with the language it seems that only any
or string
variables are supported in for
loop.
为什么做出这个决定?
为什么不使用类型信息并具有强类型迭代变量?
Why not use the type information and have strongly-typed iteration variable?
推荐答案
for-in 语句确实用于枚举对象属性,这就是它在 TypeScript 中的实现方式.在数组上使用它存在一些问题.
The for-in statement is really there to enumerate over object properties, which is how it is implemented in TypeScript. There are some issues with using it on arrays.
我不能代表 TypeScript 团队发言,但我相信这是在语言中实现的原因.
I can't speak on behalf of the TypeScript team, but I believe this is the reason for the implementation in the language.
这篇关于for-in 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!