question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Theming in typings throws an error when setting "module": "commonjs" in tsconfig.json

See original GitHub issue

DISCLAIMER:

I’m using styled-components with Electron, and I’ve found that the error is thrown when I change the tsconfig’s module from es2015 (as you guys have) to CommonJs. As I’m using Electron I need that module type. I cloned this repo and changed the module resolution and the same error arises.

Does this mean that the new styled-components typings are not compatible with Electron or rather an error on my part?

I’ve looked at the previous code I had on styledComponents.ts file and it’s exactly the same, and it was working with the “module”: “commonjs” configuration. So it must be something related to the new typings.

Here is my tsconfig.json:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es2017",
    "jsx": "react",
    "allowJs": true,
    "alwaysStrict": true
  },
  "exclude": [
    "node_modules"
  ]
}

Version

2.0.1

Reproduction

Install the new version of styled-components

Steps to reproduce

Create the styled-components file used to re-export styled:

import * as styledComponents from 'styled-components';
import { ThemedStyledComponentsModule } from 'styled-components';

export interface MyTheme {
    primaryColor: string;
    backgroundColor: string;
    defaultMargin: number;
}

const {
    default: styled,
    css,
    injectGlobal,
    keyframes,
    withTheme,
    ThemeProvider,
    // This is where the error is found
} = styledComponents as ThemedStyledComponentsModule<MyTheme>;

interface ThemeProps {
  theme?: MyTheme;
}

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

This is the same code found in the typings/tests and the one I had before, which was working as intended.

Create a theme file theme.ts:

import { MyTheme } from './../constants/styledComponents'

const theme: MyTheme = {
  primaryColor: 'mediumseagreen',
  backgroundColor: 'yellow',
  defaultMargin: 0.5
};

export default theme

Wrap App.tsx using theme provider:

import * as React from 'react'
import * as ReactDOM from 'react-dom'
import { Provider, Store } from 'react-redux'
import App from './App'
import { injectGlobal, ThemeProvider } from './constants/styledComponents'
import theme from './constants/theme'
import reducer from './reducers'
import configureStore from './store/configureStore'

const store: Store<any> = configureStore()

ReactDOM.render(
  <ThemeProvider theme={theme}>
    <Provider store={store}>
      <App />
    </Provider>
  </ThemeProvider>,
  document.getElementById('root')
)

An error is found in my styled-components.ts file:

Type 'typeof "/Users/calberca/Workspace/markers/node_modules/styled-components/typings/styled-components"' cannot be converted to type 'ThemedStyledComponentsModule<MyTheme>'.
  Types of property 'ThemeProvider' are incompatible.
    Type 'ComponentClass<ThemeProviderProps<object>>' is not comparable to type 'ComponentClass<ThemeProviderProps<MyTheme>>'.
      Type 'ThemeProviderProps<object>' is not comparable to type 'ThemeProviderProps<MyTheme>'.
        Type 'object' is not comparable to type 'MyTheme'.
          Property 'primaryColor' is missing in type '{}'.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
Igorbekcommented, Jun 9, 2017

So the actual workaround could be to write this:

styledComponents as ThemedStyledComponentsModule<any> as ThemedStyledComponentsModule<MyTheme>
0reactions
Fenopiucommented, Jan 22, 2019

Doesn’t sound like there’s anything here for us to fix, so I’ll close this issue. Lmk if that’s incorrect!

The official Typescript example still doesn’t work.

“styledComponents as ThemedStyledComponentsModule<any> as ThemedStyledComponentsModule<MyTheme>” is a workaround, not a solution. If it have to be manually converted to “any” before the custom type there is a bug. Even the recommended convention “const Title = styled(“h1”)<{ isActive: boolean }>`” doesn’t work because “styled(…)” doesn’t accept any “>” operator (Operator ‘>’ cannot be applied to types ‘boolean’ and ‘string’.).

Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Reference - Docs on every TSConfig option
Intro to the TSConfig Reference​​ A TSConfig file in a directory indicates that the directory is the root of a TypeScript or JavaScript...
Read more >
Can't import CSS/SCSS modules. TypeScript says "Cannot ...
TypeScript does not know that there are files other than .ts or .tsx so it will throw an error if an import has...
Read more >
Common TypeScript module problems and how to solve them
Build processes in TypeScript can become quite complex when we have to configure our project flow manually through the tsconfig.json file.
Read more >
Trying TypeScript | Better world by better software
TypeScript compiler should generate source maps. Go to tsconfig.json and add sourceMap setting. tsconfig.json. 1
Read more >
Getting started with React and TypeScript - Felix Gerschau
Dependencies; React tsconfig.json setup; React tsconfig.json example ... setting, which will make the compiler throw an error when we forget ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found