Material-ui: Using withTheme() with typescript

Created on 6 Sep 2017  路  5Comments  路  Source: mui-org/material-ui

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
typescript

Most helpful comment

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! 馃檪

All 5 comments

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! 馃檪

This is work great. But what if I want add my custom property in theme... TS tell me that my custom property does not exist.

Please check out the typings. If you want to add properties to the basic MUI theme you can to that, everything expects a T extends Theme.

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

Thanks, but this is a team effort! 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chadobado picture chadobado  路  119Comments

kybarg picture kybarg  路  164Comments

sjstebbins picture sjstebbins  路  71Comments

garygrubb picture garygrubb  路  57Comments

iceafish picture iceafish  路  62Comments