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.

Wrong TS type infered for object created using createUseStyles

See original GitHub issue

Expected 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:open
  • Created 2 years ago
  • Reactions:2
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
pip8786commented, Mar 29, 2021

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.

0reactions
ITenthusiasmcommented, Mar 29, 2021

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.

Read more comments on GitHub >

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

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