styled components theming typescript code example

Example 1: styled components props typescript

# to prevent typescript error, install this
yarn add @types/styled-components

# NPM
npm install @types/styled-components

Example 2: styled components default theme typescript

// import original module declarations
import 'styled-components';

// and extend them!
declare module 'styled-components' {
  export interface DefaultTheme {
    borderRadius: string;

    colors: {
      main: string;
      secondary: string;
    };
  }
}

Example 3: styled components props typescript

interface YourProps {
  invalid: boolean
}

const Input = styled.input`
  border: ${(p: YourProps) => p.invalid ? 'red' : 'blue'};
`