[TypeScript] Currently not possible to use business variables in a theme with "withStyles"
See original GitHub issueI’m using TypeScript and I’m trying to define custom business variables in my theme, as described in the docs here.
This is what I have so far:
interface MyThemeExtension {
status: {
danger: string
};
}
export const theme = createMuiTheme<MyThemeExtension>({
status: {
danger: orange[500]
}
});
export type MyTheme = Theme<MyThemeExtension>;
But now I’m having trouble to use the withStyles
HoC, because the theme
argument of StyleRulesCallback
is of type Theme
. I’m no TS expert, but I think the types of Theme
and StyleRulesCallback
need to be changed to something like this (at least this worked for me):
-type StyleRulesCallback<ClassKey extends string = string> = (theme: Theme) => StyleRules<ClassKey>;
+type StyleRulesCallback<ClassKey extends string = string, T = {}> = (theme: Theme<T>) => StyleRules<ClassKey>;
-function withStyles<ClassKey extends string>(
+function withStyles<ClassKey extends string, T = {}>(
- style: StyleRules<ClassKey> | StyleRulesCallback<ClassKey>,
+ style: StyleRules<ClassKey> | StyleRulesCallback<ClassKey, T>,
options?: WithStylesOptions
): <P>(
component: React.ComponentType<P & WithStyles<ClassKey>>
) => React.ComponentType<P & StyledComponentProps<ClassKey>>;
These changes allowed me to do: withStyles((theme: MyTheme) => ({...})
Again, I’m not sure if this is the correct way. Please let me know.
Your Environment
Tech | Version |
---|---|
Material-UI | 1.0.0-beta.21 |
React | 16.1.1 |
TypeScript | 2.5.3 |
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
[TypeScript] Currently not possible to use business variables ...
I'm using TypeScript and I'm trying to define custom business variables in my theme, as described in the docs here. This is what...
Read more >ReactJS Material UI Typescript - withStyles warning
The props interface should be extended refer to official document import { WithStyles, withStyles, withTheme } from '@material-ui/core'; ...
Read more >5 Ways to Improve Your React Native Styling Workflow
The theme in React's context will make sure that whenever the app changes between light and dark mode, all components that access the...
Read more >TypeScript - Material-UI
Using withStyles in TypeScript can be a little tricky, but there are some utilities to make the experience as painless as possible.
Read more >How to use styled components with Material UI in a React app
Even though our objective is to style our web app using styled-components, we should not be afraid of using Material UI's theme feature....
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 FreeTop 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
Top GitHub Comments
Thanks for the insight, @pelotom! I totally agree that the proposed solution with regards to type safety doesn’t make much sense, and so I will just import and use the theme directly, instead of using a callback with withStyles. Same for custom business variables, I’ll just import them wherever I need them.
So this issue can be closed from my side.
@gariggio I suggest taking a look at this page for the latest best practice for customizing the theme in TypeScript:
https://material-ui-next.com/guides/typescript/#customization-of-theme