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.

injectSheet typings require very verbose usage currently

See original GitHub issue

With the latest 10.0.x typings, any TypeScript usages of injectSheet require very verbose explicit type parameters to get the outer component type right. The biggest pain is that InjectedProps<Styles, Theme> is not exposed, so one has to declare the inner component’s Props as Props & { classes: { [key: string]: string } }.

My suggestion for this would be to expose InjectedProps as WithStyles or a similar name (following the naming convention of packages such as material-ui) and to add defaults to the generic parameters, so the inner styled component could be declared as Props & WithStyles.

The next challenge is that injectSheet itself receives the generic Props type, and passes it to its return type, so TypeScript can’t infer the inner Props from usage. This means that in order to get the correct outer props, injectSheet has to be called as injectSheet<Styles, {}, Props & { classes: { [key: string]: string } }>( styles )( Component ).

My suggestion for this would be to change the definition for injectSheet so that the generic Props type argument is declared on the return type like this:

export default function injectSheet<
  Style extends Styles,
  Theme extends object
>(
  styles: Style,
  options?: Options
): <Props extends InjectedProps<Styles, Theme>>(
  comp: React.ComponentType<Props>
) => React.ComponentType<Omit<Props, InjectedProps<Styles, Theme>>>

That way TypeScript can infer the type of Props from the usage when the return value from injectSheet is called with a Component.

I’d be happy to make these changes in a PR if there is no opposition to them or a better way to work around the current issues.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
HenriBeckcommented, Jan 10, 2019

Should be fixed now, I forgot to omit the props before adding them as optional again.

Usage:

import withStyles, { WithStyles } from 'react-jss';

const styles = {};

interface Props extends WithStyles<typeof styles> {
  isDisabled: boolean,
}

function Comp(props: Props) {
  return <button disabled={props.isDisabled}></button>
}

export default withStyles(styles)(Comp)
1reaction
HenriBeckcommented, Jan 8, 2019

I updated the react-jss types: https://github.com/cssinjs/jss/blob/react-jss/update/packages/react-jss/src/index.d.ts

Feel free to have a look; those types should be published by the end of the week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSSinJS - Design Lance
It is slightly more verbose, of course; after all, it is pure TypeScript that doesn't require any ... We use styles.significant.name as a...
Read more >
JSS integration with React
If you want to whitelist some of them, you can now use option inject . For e.g. if you want to access the...
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