-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
Wrong TS type infered for object created using createUseStyles #1479
Comments
First off, thank you so much for adding a codesandbox. Plus the example code. Makes life much easier. Ugh. Yeah that really bites. The issue is that TypeScript is trying to infer what I'm looking into this. I can't guarantee that a simple fix will be possible; but hopefully I (or someone else) can find one. In the meantime, there's workaround: const useStyles = createUseStyles((theme) => ({
SectionTitle: (props: JssProps) => ({
color: theme.color,
// Bullet
"&:before": {
content: `"${props.number}"`,
border: [1, "solid", "black"]
}
})
})); If you can clarify the Pro tip for the future: |
UPDATE Here's another workaround that I discovered: const useStyles2 = createUseStyles((theme) => ({
SectionTitle: {
color: theme.color as string,
// Bullet
"&:before": {
content: (props: JssProps) => `"${props.number}"`,
border: [1, "solid", "black"]
}
}
})); This one is much less inconvenient, visually. 😄 (At least in my opinion. 😅) For some reason, TypeScript seems to get really confused when the type you give to a CSS Property ( |
Another update. (This update likely won't interest anyone searching for workarounds/fixes.) I played around with this code, and apparently it produces the same error: const useStyles = createUseStyles((theme) => ({
SectionTitle: {
color: '' as any,
// Bullet
'&:before': (props: MyProps) => ({
content: `"${props.property}"`,
border: [1, 'solid', 'black']
})
}
})) Apparently, if the Of course, no one who's explicitly providing the necessary generic type arguments will run into this issue. This is the extent of my investigation for now. I'll try to look into it more later. But seeing as TS is getting upset specifically when the CSS Properties are typed unpredictably, I don't know how deeply this should be explored. @MaximeCheramy you can let me know if you have any thoughts about any of this. I hope the workarounds I provided work for you in the meantime. |
Not sure if this the right place to comment about the 10.6 parameter changes, but we've downgraded to 10.5 to avoid all the errors for now until this is figured out. I wonder if we can look at what Material UI did to wrap react-jss because IIRC you'd get |
@pip8786 Are you getting this same exact error? Or are you just talking about the re-ordering of parameters? |
I'm getting a lot of errors of this type: |
Do you have a code snippet for more context? |
Here's one of the smaller examples I could find:
|
Okay, thanks. And just for clarity... Let's say your props were named const useStyles = createUseStyles<"pre", MyProps>({
pre: {
fontFamily: '"Roboto", "Helvetica", "Arial", "sans-serif"',
whiteSpace: "pre-wrap",
margin: 0,
color: ({color}) => color ? color : "white",
fontWeight: ({weight}) => weight ? weight : 400,
wordWrap: ({breakWords}) => breakWords ? "break-words" : "inherit"
}
}); |
Yes that fixes it but like I said, this was one simple example of many places where this errors out. I'm not sure I want to specify every single classname coming out of the createUseStyles() just to get typed props. Based on the discussions i read around this issue, it sounds like this may be avoidable in the future if Typescript fixes some issues, so I think for our project we rather wait until that happens and stick to 10.5.0 for now. Was I wrong about Material UI having figured this out? I know I didn't specify class names coming out of that but had props completion as well classnames, I think. |
Yes, I understand. I was mainly asking to get a handle on what the actual problem was on your end. Your problem seems to be separate from the one in this issue.
I'm uncertain. I've only started to get some familiarity with this library, and I haven't dived too deep into Material UI's code. It's possible they either solved the problem React-JSS faces or found a workaround (and required users to use that workaround). Given the complexity of this issue though, I have my doubts. Are you sure they use React-JSS? Or do they just use JSS? |
It appears they use JSS: I just tested it on another project I have that uses Material UI and it doesn't seem to autocomplete the class names, though it does have props checking. Looks like it works a lot like the previous version of react-jss based on what I'm testing and see here: https://github.com/mui-org/material-ui/blob/next/packages/material-ui-styles/src/makeStyles/makeStyles.d.ts |
Yeah. I guess I'm not surprised. This is a pretty bothersome bind that TypeScript puts us in. I really really hoping it gets resolved in the near future. |
Expected behavior:
Given the following snippet:
The type should be:
But instead, it's:
Describe the bug:
This only occurs in a very specific case:
My theme is actually generated by a webpack plugin, that's why the type not precise.
Reproduction:
https://codesandbox.io/s/bug-type-jss-j5gkw
Versions (please complete the following information):
The text was updated successfully, but these errors were encountered: