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.

Typescript: Types for withStyles() - forced to set classes-property

See original GitHub issue

Usage of the withStyles()-function enforces setting the classes property.

TS doesn’t seem to split WithStyles<ClassKey> from P. This could be seen as a typescript-fault, but I think it can easily be fixed within the type definition.

export default function withStyles<ClassKey extends string>(
  style: StyleRules<ClassKey> | StyleRulesCallback<ClassKey>,
  options?: WithStylesOptions,
): <P>(
  component: React.ComponentType<P & WithStyles<ClassKey>>,
) => React.ComponentType<P & StyledComponentProps<ClassKey>>;

One could replace

<P>(
  component: React.ComponentType<P & WithStyles<ClassKey>>,
) => React.ComponentType<P & StyledComponentProps<ClassKey>>;

with

import { Omit } from 'material-ui/index';

<P extends WithStyles<ClassKey>>(
component: React.ComponentType<P>
) => React.ComponentType<Omit<P, keyof WithStyles>> & StyledComponentProps<ClassKey>;
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

  • Compile without error

Current Behavior

(55,17): error TS2322: Type ‘{}’ is not assignable to type ‘(IntrinsicAttributes & IntrinsicClassAttributes<Component<{ classes: any; } & StyledComponentProp…’. Type ‘{}’ is not assignable to type ‘IntrinsicAttributes & { classes: any; } & StyledComponentProps<never> & { children?: ReactNode; }’. Type ‘{}’ is not assignable to type ‘{ classes: any; }’. Property ‘classes’ is missing in type ‘{}’.

Steps to Reproduce (for bugs)

const Test = withStyles({})((props: { classes: any }) => <div/>);
const App = () => <Test />

Your Environment

Tech Version
Material-UI 1.0.0-beta.30
React 16.2.0
browser none / compile-error
typescript 2.6.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

6reactions
xcaptaincommented, Apr 6, 2018

I also came across this problem, I’m using typescript 2.8.1, solved by using

const styles = {root: {}};
interface TestProps {
    classes?: any;
}
const Test = withStyles(styles)<TestProps>(props => (
<div className={props.classes.root}>Hello</div>
));
const App = () => <Test />

and set no-any: false in tslint.json, no need to change tsconfig.json.

But still looking forward a better solution.

5reactions
laaksomavrickcommented, Mar 3, 2019

I was having issues using withStyled as a HOC with stateless components. I fixed the issue as follows:

const styled = withStyles(theme => ({
  container: {...},
  paper: {...},
}));

interface Props extends HTMLAttributes<HTMLDivElement> {
  classes: any;
}

const StatelessComponent = ({ classes, children }: Props) => (
  <div className={classes.container}>
    <Paper className={classes.paper}>{children}</Paper>
  </div>
);

export default styled(StatelessComponent);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: Types for withStyles() - forced to set classes ...
Usage of the withStyles()-function enforces setting the classes property. TS doesn't seem to split WithStyles from P. This could be seen as ...
Read more >
Typescript Material UI Property 'classes' is missing in type for ...
TS2741: Property 'classes' is missing in type '{}' but required in type 'Props'. Here is a template which shows how it can be...
Read more >
Advanced (LEGACY) - MUI System
The makeStyles (hook generator) and withStyles (HOC) APIs allow the creation of multiple style rules per style sheet. Each style rule has its...
Read more >
CHANGELOG.md - TINMAN
We are making a quick release to fix an important TypeScript regression. ... of tab (#12706) @adeelibr - [typescript] Set default for withStyles'...
Read more >
Typing React with typescript - DEV Community ‍ ‍
Had we used the Props type instead of PublicProps we would get a pesky TypeScript error complaining about classes property missing.
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