我有一个从 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是 …