javascripttypescriptstyled-componentsthemeprovider

ThemeProvide used with Typescript is giving an error


I want to create a component

const StyledDiv = styled.div`
width: 10rem;
height: 3rem;
border-radius: 0.2rem;
background-color: ${({ theme, colorVariant, colorType }) => theme.colors[colorType]. 
 [colorVariant]}
`;

But is showing an error

Property 'colorVariant' does not exist on type 'Pick<DetailedHTMLProps<HTMLAttributes, HTMLDivElement>, "slot" | "style" | "title" | ... 252 more ... | "css"> & { ...; } & ThemeProps<...>'.

This is the Theme that i have set up

export default {
  'colors' : {
    'blue' : {
      '100' : '#232f3e',
      '200' : '#233d51',
      '300' : '#006170',
      '400' : '#008296',
      '450' : '#008091',
      '500' : '#d4e4e6',
      '600' : '#e5f2f3',
    },
    'grey' : {
      '600' : '#8ca1a3',
      '700' : '#768283',
      '800' : '#e7e9e9',
      '900' : '#ffffff',
    },
    'red' : {
      '1000' : '#b40909',
    },
    'green' : {
      '1100' : '#417505',
    },
    'yellow' : {
      '1200' : '#f5a623',
    }
  }
};

Solution

  • Are you using styled-components? If so, it seems the div method also takes an argument as extended props what you need. This is what I meant:

    const StyledDiv = styled.div<{ colorVariant: string, colorType: string }>`
      width: 10rem;
      height: 3rem;
      border-radius: 0.2rem;
      background-color: ${({ theme, colorVariant, colorType }) => theme.colors[colorType]. 
       [colorVariant]}
    `;