vue3 ts 无法这样设置值 ObjectkeystoRefsglobalConfigureforEachkey = if changeAttribute === key globalConfigurekey = change 报 TS7053 Element implicitly has an any type because expression of type
这个错误是因为 TypeScript 不知道对象中是否存在与字符串索引相对应的属性。可以通过使用索引签名来解决这个问题。在对象类型中添加一个索引签名,允许任意字符串作为属性名,同时指定属性值的类型。例如:
interface GlobalConfigure {
[key: string]: boolean;
SetTop: boolean;
AutomaticStartUp: boolean;
RemoveSource: boolean;
RemoveRestore: boolean;
}
const globalConfigure: GlobalConfigure = {
SetTop: false,
AutomaticStartUp: true,
RemoveSource: true,
RemoveRestore: false,
}
Object.keys(toRefs(globalConfigure)).forEach((key) => {
if (changeAttribute === key) {
globalConfigure[key] = change
}
})
在上述代码中,我们定义了一个名为 GlobalConfigure 的接口,它具有一个字符串索引签名,允许使用任意字符串索引对象,并指定索引值的类型为 boolean。然后,我们将对象 globalConfigure 的类型设置为 GlobalConfigure,并使用 Object.keys() 循环遍历对象的属性。在循环中,我们可以通过索引 globalConfigure 对象来设置属性的值,而不会出现编译错误
原文地址: https://www.cveoy.top/t/topic/eiPZ 著作权归作者所有。请勿转载和采集!