Skip to content

[react-jss] createUseStyles has strange types in TypeScript #1203

@goloroden

Description

@goloroden

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions