Search code examples
javascriptreactjstypescriptreact-typescriptecmascript-2016

React typescript error: Element implicitly has an 'any' type


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]


Solution

  • Limit color to be a key of colors:

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