我已经解决了许多其他有类似错误的问题,但没有看到我的代码有什么问题。你能确定如何修复ts(7053)错误。我收到的完整错误是Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ card: number; dialog: number; navigation: number; layer: number; blanket: number; modal: number; flag: number; spotlight: number; tooltip: number; }'.代码是const zIndexToName: Record<number, string> = Object.keys(layerIndex).reduce( (acc: Record<number, string>, name: string) => { const value: number = layerIndex[name](); acc[value] = name; return acc; }, {});鉴于,layerIndex是export const layerIndex = { card: 100, dialog: 300, navigation: 200, layer: 400, blanket: 500, modal: 510, flag: 600, spotlight: 700, tooltip: 800};我可以全部切换string到any但得到相同的错误,Element implicitly has an 'any' type because expression of type 'any' can't be used to index typeconst zIndexToName: Record<number, any> = Object.keys(layerIndex).reduce( (acc: Record<number, any>, name: any) => { const value: number = layerIndex[name](); acc[value] = name; return acc; }, {});
1 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
您缺少 的索引类型layerIndex
。
export const layerIndex: { [key: string]: number }
我认为对号码的函数调用是错误的。它应该是
const value: number = layerIndex[name];
添加回答
举报
0/150
提交
取消