元素隐式地具有“ any”类型,因为类型“ any”的表达式不能用于索引类型

时间:2019-10-24 14:47:41

标签: javascript reactjs typescript

我遇到了许多其他问题,这些问题都有类似的错误,但是看不到我的代码有什么问题。您能确定如何解决{{ form.textarea('desc', id='field-description', label=_('Description'), value=data.desc, error=errors.desc, classes=['your-class') }} 错误吗?

我收到的完整错误是

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 type

1 个答案:

答案 0 :(得分:1)

您缺少layerIndex的索引类型。

export const layerIndex: { [key: string]: number }

我认为对数字的函数调用是一个错误。应该是

 const value: number = layerIndex[name];