ThemeProvide used with Typescript is giving an error

1.1k views Asked by At

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',
    }
  }
};
2

There are 2 answers

0
tmhao2005 On BEST ANSWER

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]}
`;
0
anandharshan On

One way i have found out is


interface IStyledColorBar {
  colorVariant: string,
  colorType: string,
};

type StyledProps<T = {}> = React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement> & Partial<T>, HTMLDivElement>;

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

I got this solution from https://github.com/emotion-js/emotion/issues/802#issuecomment-444181912