patrick91 / Styled-Components-Typescript-Example

Sample "app" using typescript and styled components.
104 stars 27 forks source link

TSLint throws error with this example here: https://www.styled-components.com/docs/api#typescript #4

Closed alveshelio closed 6 years ago

alveshelio commented 6 years ago

Hi,

I'm trying to get styled-components to work with TypeScript but I'm facing some issues. I've followed this example here: https://www.styled-components.com/docs/api#typescript

In this code:

import ThemeInterface from './theme';

const {
  default: styled,
  css,
  injectGlobal,
  keyframes,
  ThemeProvider,
} = styledComponents as ThemedStyledComponentsModule<ThemeInterface>;

export { css, injectGlobal, keyframes, ThemeProvider };
export default styled;

Typescript is throwing the following error:

   TS2352: Type 'typeof "/Users/xxxx/xxxxx/React/TypeScriptWithReact/node_modules/styled-components/t...' cannot be converted to type 'ThemedStyledComponentsModule<ThemeInterface>'.
  Types of property 'ThemeProvider' are incompatible.
    Type 'ComponentClass<ThemeProviderProps<object>>' is not comparable to type 'ComponentClass<ThemeProviderProps<ThemeInterface>>'.
      Type 'ThemeProviderProps<object>' is not comparable to type 'ThemeProviderProps<ThemeInterface>'.
        Type 'object' is not comparable to type 'ThemeInterface'.
          Property 'primaryColor' is missing in type '{}'.

My theme.ts file looks like this:

export default interface ThemeInterface {
  primaryColor: string;
  primaryColorInverted: string;
}

Also, I'd like to know if I need to import styled from this file styled-components.ts or do I need to still import styled from 'styled-components' from node-modules?

Any help would be appreciated. Thank you

patrick91 commented 6 years ago

@alveshelio hey! sorry for the delay, I just pushed an example of using the theme with typescript, hope it helps!

https://github.com/patrick91/Styled-Components-Typescript-Example/commit/6bcb447c09a299390d0b5a7488feaf5065f328d7