Using withTheme() with typescript
See original GitHub issueProblem 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:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 thewithTheme
option! 🙂@sebald you rock 🤘🏻. I rarely had so much fun coding than since using v1-beta with TS.