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

我已经解决了许多其他有类似错误的问题,但没有看到我的代码有什么问题。你能确定如何修复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


const zIndexToName: Record<number, any> = Object.keys(layerIndex).reduce(

  (acc: Record<number, any>, name: any) => {

    const value: number = layerIndex[name]();

    acc[value] = name;

    return acc;

  },

  {}

);


开满天机
浏览 5455回答 1
1回答

鸿蒙传说

您缺少 的索引类型layerIndex。export&nbsp;const&nbsp;layerIndex:&nbsp;{&nbsp;[key:&nbsp;string]:&nbsp;number&nbsp;}我认为对号码的函数调用是错误的。它应该是&nbsp;const&nbsp;value:&nbsp;number&nbsp;=&nbsp;layerIndex[name];
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript