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.

[TypeScript] Currently not possible to use business variables in a theme with "withStyles"

See original GitHub issue

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 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:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
simonvizzinicommented, Nov 29, 2017

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.

0reactions
pelotomcommented, Mar 20, 2018

@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

Read more comments on GitHub >

github_iconTop 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 >

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