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.

[react-jss] createUseStyles has strange types in TypeScript

See original GitHub issue

We (that is @mattwagl, @yeldiRium and me) have recently migrated our module’s dependencies jss and react-jss to 10.0.0, and have tried to convert from the HOC approach withStyles function to the hook-based approach with createUseStyles.

Since we are using theming, we would like to use createUseStyles with a function parameter, basically like this:

const useStyles = createUseStyles(theme => ({
  // ...
}));

Problems start when we look at the types of the useStyles function.

Expected behavior:

What we expect is that useStyles should return something of type Styles, or at least something such as Record<string, string>.

Actual behavior:

What it actually returns is Record<never, string>. We don’t know where this never comes from, but obviously this is not what it should be 😉

Workaround

We are able to fix this by using

const useStyles = createUseStyles<string>(theme => ({
  // ...
}));

but this for sure can’t be the right way to go. Actually, it even makes us wonder that this works, since createUseStyles takes two type parameters when providing a function. Something with the typings seems to be broken here (or we missed something very essential with respect to this new hook-based approach).

System environment

  • Node.js 12.10.0
  • macOS 10.14.6 (Mojave)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
RPDeshaiescommented, Oct 31, 2019

I will leave this here in case people want a temporary typing solution while this is being worked on but this is what I was able to come up with this morning.

I had to override the return type of the default function with a as any but I guess the team at jss can maybe get some inspiration from the following code:


const styleFunction = theme => ({
  image: {
    display: "flex",
    width: "100%"
  }
});

// OR

const styleObject = {
  image: {
    display: "flex",
    width: "100%"
  }
};

function betterCreateUseStyles<TStyle>(
  style: TStyle
): TStyle extends (...args: any) => any
  ? (data?: any) => Record<Extract<keyof ReturnType<TStyle>, string>, string>
  : (data?: any) => Record<Extract<keyof TStyle, string>, string> {
  return createUseStyles(style) as any;
}

/**
 * `(data: any) => { image: string; }`
 */
betterCreateUseStyles(styleFunction);

/**
 * `(data: any) => { image: string; }`
 */
betterCreateUseStyles(styleObject);

EDIT

I just created myself a typings/react-jss.d.ts where I added

declare module "react-jss" {
  export function createUseStyles<TStyle>(
    style: TStyle
  ): TStyle extends (...args: any) => any
    ? (data?: any) => Record<Extract<keyof ReturnType<TStyle>, string>, string>
    : (data?: any) => Record<Extract<keyof TStyle, string>, string>;
}

export * from "react-jss";

3reactions
HenriBeckcommented, Jan 3, 2020

cc @HenriBeck we could revert it in the 10.0.4 release and republish as 11

I don’t think this change deservers a complete major release. We only introduced these types recently here in the main repo.

We could add a type alias and mark that one as deprecated. That would be the correct solution here, in my opinion.

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 >
Create styles using JSS and TypeScript in react apps. With ...
At this stage, we have to implement our style. Styles.ts import { createUseStyles } from "react-jss";const getStyles = { button: { borderRadius: ...
Read more >
JSS integration with React
A lib for generating CSS from JavaScript.
Read more >
5 things you didn't know you can do in CSS-in-JS
You know CSS-in-JS lets you style applications with JavaScript, but you can ... createUseStyles, ThemeProvider, useTheme } from "react-jss"; ...
Read more >
Exploring CSS in JS - lee.david.cs
CSS-in-JS (or CiJ) refers to the technique of using JavaScript to write styles to generate ... 4import { createUseStyles } from "react-jss";.
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