我有这个代码:
const sectionInstance: FieldValues = sectionInstances[i]
for (const field in sectionInstance) {
console.log(sectionInstance[field])
}
这里的field
当然是一个字符串。以下是 FieldValues
的类型定义:
export interface FieldValues = {
[key: string]: FieldValue;
}
我仍然收到此错误:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'FieldValues'. No index signature with a parameter of type 'string' was found on type 'FieldValues'.Vetur(7053)
我不是已经为接口(interface) FieldValues
声明了“字符串”类型的索引签名吗?为什么我会收到此错误?
最佳答案
只需从接口(interface)定义中删除 =
即可。
Please take a look at playground
interface FieldValues {
[key: string]: FieldValue;
}
...
const sectionInstance: FieldValues = sectionInstances[i];
for (const field in sectionInstance) {
console.log(sectionInstance[field]);
}
关于javascript - 接口(interface)上类型 'string' 的索引签名无法识别( typescript ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63218725/