gpt4 book ai didi

javascript - 接口(interface)上类型 'string' 的索引签名无法识别( typescript )

转载 作者:行者123 更新时间:2023-12-04 09:15:54 33 4
gpt4 key购买 nike

我有这个代码:

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 indexsignature 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com