Question regarding latest (beta.43) WithStyles update.
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior
Sample code to compile.
Current Behavior
does not compile with that error
Type '{ caption: string; }' has no properties in common with type 'IntrinsicAttributes & StyledComponentProps<"content"> & { children?: ReactNode; }'.
Steps to Reproduce (for bugs)
- Try to compile the sample code
import { withStyles, WithStyles } from 'material-ui';
import * as React from 'react';
import './App.css';
interface IStyle {
content: any;
}
interface IComponentProps {
caption: string;
}
type ComponentProps = IComponentProps & WithStyles<'content'>;
const decorate = withStyles((theme): IStyle => ({
content: {
margin: 4
}
}));
const Component = (props: ComponentProps) => {
return <div className={props.classes.content}>Hello {props.caption}</div>
}
const StyledComponent = decorate(Component);
class App extends React.Component {
public render() {
return (
<div className="App">
<StyledComponent caption="Developer" />
</div>
);
}
}
export default App;
Context
The sample code was compiling fine before beta.43 update. Now I need to change
const StyledComponent = decorate(Component);
line as
const StyledComponent = decorate<IComponentProps>(Component);
to make it compile without errors.
I’m not sure if it is a bug or an intentional change so I wanted to ask before attempting to change my all styled components. I think this probably relates with https://github.com/mui-org/material-ui/pull/11003
Your Environment
Tech | Version |
---|---|
Material-UI | v1.0.0-beta.43 |
React | 16.3.2 |
browser | Chrome |
typescript | 2.8.3 |
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:38 (35 by maintainers)
Top Results From Across the Web
withStyles(styles) in react app giving Syntax error: Unexpected ...
The project runs fine in codesandbox, but when run locally, its giving error at line Unexpected token > @withStyles(styles) Steps to ...
Read more >Material-UI: "The key provided to the classes property is not ...
Since Items is receiving this prop, it's reading it as you trying to override classes that aren't available and warning you about it....
Read more >@material-ui/core | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >Module not found: Error: Can't resolve '@material-ui/core/styles
Problem description Hi, in material-ui@next import getMuiTheme from 'material-ui/styles/getMuiTheme'; Module not found: Error: Can't resolve ... Favicon for you ...
Read more >Changelog - React Magma
buttons: update buttons for new space scale (2822993) ... combobox: set input value on new item creation (7115817) ... 2.0.0-beta.43 (2020-05-12). Features.
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 have a solution which solves all known edge cases, but requires TypeScript 2.8. See #11280, and feel free to comment there. I’d like to get a sense from the users whether 2.8 is an acceptable baseline.
Ok, let me explain a bit.
The reason it fails without
strictFunctionTypes
in the theme case is because functions are bivariantly equivalent in that case, and this affects whether the first alternative in the withStyles overload pair is deemed valid.It reads
and question is whether the parameter declaration in usage
matches the withStyles declaration’s parameter
With bivariance (without
strictFunctionTypes
) Typescript asks the wrong question: it asks:which of course is true, but not the right question. With contravariance (with
strictFunctionTypes
), the opposite question is asked:which is false, and what we want.
This may seem backward, until you think about it in the context of being a function argument, which is exactly what
strictFunctionTypes
is about. See https://www.stephanboyer.com/post/132/what-are-covariance-and-contravariance for a good writeup. Abbreviated, the question is:With
strictFunctionTypes
, the answer is “No, we only do Dogs (or any subtype)”; without it, the answer is “Sure, it might work”.