小编Emr*_*rio的帖子

Typescript 元素隐式具有类型 any 和 for...in 循环

我有一个从 JSON 文件(带有resolveJsonModule: true)导入的 JSON 对象。该对象如下所示:

"myobject": {
  "prop1": "foo",
  "prop2": "bar"
}
Run Code Online (Sandbox Code Playgroud)

因此它的类型如下所示:

"myobject": {
  "prop1": "foo",
  "prop2": "bar"
}
Run Code Online (Sandbox Code Playgroud)

这很好,但是当我尝试使用for...in循环时,

myobject: { prop1: string, prop2: string }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ "prop1": string; "prop2": string; }'.
  No index signature with a parameter of type 'string' was found on type '{ "prop1": string; "prop2": string; }'.
Run Code Online (Sandbox Code Playgroud)

我知道这意味着迭代器key是 …

for-in-loop object-property typescript

0
推荐指数
2
解决办法
1056
查看次数

标签 统计

for-in-loop ×1

object-property ×1

typescript ×1