-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TypeScript] Currently not possible to use business variables in a theme with "withStyles" #9192
Comments
There's a small problem with the proposed solution, which is that it isn't type safe. Since the place where the global theme is defined is divorced from the place where we want to use it, there's no way to enforce that your global theme is a withStyles((theme: Theme<{ foo: { bar: number } }>) => ({
root: {
lineHeight: theme.foo.bar,
},
})); It may be an ok compromise to make, I just want to point out that it's really no safer than doing a cast like this: withStyles(theme => ({
root: {
backgroundColor: (theme as MyTheme).status.danger,
},
})); In general this is making me wonder why there is any advantage to using // src/theme.ts
import createMuiTheme from 'material-ui/styles/createMuiTheme';
import { red, purple } from 'material-ui/colors';
export default createMuiTheme({
palette: {
primary: red,
secondary: purple,
},
myCustomScalar: 3,
}); // src/component.ts
import * as React from 'react'
import { withStyles } from 'material-ui/styles'
import theme from './theme'
export default withStyles({
root: {
margin: theme.spacing.unit * theme.myCustomScalar
}
})(...); |
Deleted some comments that were not pertinent to the issue. |
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. |
Even though these bussines variables are theme related? |
@gariggio I suggest taking a look at this page for the latest best practice for customizing the theme in TypeScript: https://material-ui.com/guides/typescript/#customization-of-theme |
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:
But now I'm having trouble to use the
withStyles
HoC, because thetheme
argument ofStyleRulesCallback
is of typeTheme
. I'm no TS expert, but I think the types ofTheme
andStyleRulesCallback
need to be changed to something like this (at least this worked for me):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
The text was updated successfully, but these errors were encountered: