Wrong TS type infered for object created using createUseStyles
See original GitHub issueExpected behavior:
Given the following snippet:
const useStyles = createUseStyles((theme) => ({
SectionTitle: {
color: theme.color, // Commenting out this line fixes the bug
// Bullet
"&:before": {
content: (props: JssProps) => `"${props.number}"`,
border: [1, "solid", "black"]
}
}
}));
The type should be:
const useStyles: (data?: JssProps & {
theme?: Jss.Theme;
}) => Record<"SectionTitle", string>
But instead, it’s:
const useStyles: (data?: {
theme?: Jss.Theme;
}) => Record<"SectionTitle", string>
Describe the bug:
This only occurs in a very specific case:
- We use the theme in the JSS
- The Theme is not correctly defined (any)
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):
- jss: 10.6.0
- Browser [e.g. chrome, safari]: Chrome / FireFox
- OS [e.g. Windows, macOS]: Linux
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:13 (8 by maintainers)
Top Results From Across the Web
TypeScript Typings with createUseStyles from react-jss
I think I've found the solution. The passing of theme/props to the classes still feels strange to me but it works and satisfies...
Read more >TypeScript - Emotion
These definitions infer types for css properties with the object syntax, HTML/SVG tag names, and prop types. @emotion/react. The easiest way to use...
Read more >jsstyles - Bountysource
Describe the bug: I'm creating a useStyles hook where the rules are all derived from prop values: const useStyles = createUseStyles({ styles: {...
Read more >TypeScript errors and how to fix them
error TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier ... Consider using a mapped object type...
Read more >Create styles using JSS and TypeScript in react apps. With ...
You probably know and use a variety of ways to write styles in React apps, but today I want to show you a...
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
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.
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.