Home > Mobile >  React typescript error: Element implicitly has an 'any' type
React typescript error: Element implicitly has an 'any' type

Time:02-05

I am a beginner at typescript and I am using it in reactjs but I am getting errors. Could someone please help me with how to resolve this issue? Element implicitly has an 'any' type because expression of type 'string' can't be used to index type.

export const colors = {
  RootBackground: "#fff",
}



style = {
  [
    buttonStyle ? buttonStyle : styles.button,
    {
      backgroundColor: colors[color],
      width
    },
  ]
}

Note: Error is on this line colors[color]

CodePudding user response:

Limit color to be a key of colors:

interface Props {
  color: keyof typeof colors
  // other props
}
  •  Tags:  
  • Related