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.

WithStyles<typeof styles> not working properly

See original GitHub issue

WithStyles<typeof styles> doesn’t get along with with TS (no auto completion and I get type mismatch error).

  • This is a v1.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

New Material UI Core’s WithStyles<> should be able to receive the the type of styles object somehow, for further auto-completion of classes from this.props.classes.

It works only if I hardcode the class names (i.e. WithStyles<'className'>), but it contradicts DRY principle.

Current Behavior

I tried WithStyles<typeof styles>, WithStyles<keyof typeof styles>, but nothing works.
I don’t get auto-completion for the classes and also type checking fails using withStyles:

const styles = (theme: Theme) => createStyles({
    someClass: 
});
interface Props extends WithStyles<typeof styles> {
   prop: string
}
class SomeClass extends React.Component<Props> {... }

export default withStyles(styles)<Props>(SomeClass);

Tried also without createStyles or just withStyles(styles)(SomeClass), but nothing works.

Using WithStyles<keyof typeof styles>, I get no classes auto-completion, but types match. Using WithStyles<typeof styles>, no auto completion and type mismatch error in withStyles:

...
Type SomeClass no match for signature
'(props: Props & Partial<WithTheme> & { classes: Record<string, string>;
string? any; T: string; StyleRulesCallback<infer, K>(): any ...}'

Steps to Reproduce

Written above.

Context

I can proceed with development by hardcoding all classnames or giving up on type checking, but it’s frustrating.

Your Environment

Tech Version
Material-UI v1.2.0
React v16.4.0
Typescript v2.9.2

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
pelotomcommented, Aug 6, 2018

@franklixuefei I see, makes sense. Good luck with the PR!

2reactions
nmenkecommented, Jul 30, 2018

WithStyles<keyof ReturnType<typeof styles>>

Read more comments on GitHub >

github_iconTop Results From Across the Web

WithStyles<typeof styles> not working properly #12334
WithStyles <typeof styles> doesn't get along with with TS (no auto completion and I get type mismatch error). This is a v1.x issue....
Read more >
Material-UI withStyles doesn't apply any kind of styles
makeStyles returns a custom hook and passing this custom hook into withStyles will not work correctly. Instead, you want the following:
Read more >
JSS integration with React
React JSS provides a WithStyles type which adds types for all of the injected props. To use it, simply extend your existing props...
Read more >
[Solved]-WithStyles<typeof styles> in Material UI?-Reactjs
const stylesV2: typeof styles = {a: 10, b: 20, c: 24} ... Next JS not properly loading Material UI styles until refresh ·...
Read more >
TypeScript
The problem is that the type of the flexDirection property is inferred as string , which is too arbitrary. To fix this, you...
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