Typescript: Types for withStyles() - forced to set classes-property
See original GitHub issueUsage 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:
- Created 6 years ago
- Comments:15 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I also came across this problem, I’m using typescript 2.8.1, solved by using
and set
no-any: false
in tslint.json, no need to change tsconfig.json.But still looking forward a better solution.
I was having issues using withStyled as a HOC with stateless components. I fixed the issue as follows: