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.

Using withTheme() with typescript

See original GitHub issue

Problem description

When I try to wrap my component withTheme() type script show me next error.

Argument of type 'ComponentClass<StyledComponentProps<{}>>' is not assignable to parameter of type 'ComponentType<StyledComponentProps<{}> & { theme: Theme<{}>; }>'.
  Type 'ComponentClass<StyledComponentProps<{}>>' is not assignable to type 'StatelessComponent<StyledComponentProps<{}> & { theme: Theme<{}>; }>'.
    Type 'ComponentClass<StyledComponentProps<{}>>' provides no match for the signature '(props: StyledComponentProps<{}> & { theme: Theme<{}>; } & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.

Steps to reproduce

Square.tsx

import * as React from 'react';
import { withStyles, withTheme, StyleRulesCallback } from 'material-ui/styles';

import Paper from 'material-ui/Paper';
import Typography from 'material-ui/Typography';

const styles: StyleRulesCallback = theme => ({
  root: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    userSelect: 'none'
    
  }
});

interface Props {
    theme: object;
    content: number;
    isRed: boolean;
}

interface ButtonClassName {
  root: string;
}

function Button(props: Props & { classes: ButtonClassName } ) {
  const red = 'red';
  const black = 'black';
  const textColor = props.isRed ? red : black;

  return (
    <Paper className={props.classes.root}  elevation={4}>
      <Typography style={{color : textColor}} type="headline">
          <h1>Hello World</h1>
        {props.content}
      </Typography>
    </Paper>
  );
}

export default withTheme(withStyles(styles)(Button));

Versions

  • Material-UI: v1-beta8
  • React: v15.6.1

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
sebaldcommented, Sep 7, 2017

That’s because you need to specify the generics. I created an example in the typings tests for you:

https://github.com/callemall/material-ui/blob/f6313c034ca9f3d9e9bb2ecdee4a8b1efeb869bd/test/typescript/styles.spec.tsx#L92-L104

BTW You can also just use only withStyles and set the withTheme option! 🙂

1reaction
wcandilloncommented, Nov 15, 2017

@sebald you rock 🤘🏻. I rarely had so much fun coding than since using v1-beta with TS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using withTheme() with typescript · Issue #8070 · mui/material-ui
When I try to wrap my component withTheme() type script show me next error. Argument of type 'ComponentClass<StyledComponentProps<{}>>' is not ...
Read more >
styled-components withTheme HOC not working with types ...
FC typescript and today I found this typescript error when trying to inject styled-component props using withTheme HOC from ...
Read more >
Theme with styled-components and Typescript - Medium
Theming in styled-components allows your app to support multiple design patterns within the same app. Imagine an amazing code editor with no ...
Read more >
TypeScript - Theme UI
This guide is intended to cover those use cases. Exact theme type. The Theme type represents all possible themes. It might be what...
Read more >
How to create a Theme in React/Typescript (Context API) with ...
Hello everyone, in this super fast tutorial, I'll teach you how to create a theme in a React/Typescript application with styled-components, ...
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