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'.
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));
That's because you need to specify the generics. I created an example in the typings tests for you:
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! 馃檪
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
withStylesand set thewithThemeoption! 馃檪