-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WithStyles<typeof styles> not working properly #12334
Comments
Edit: Never mind all that nonsense. Where do you expect autocompletion? When you call Could you provide a complete repro? Yours has currently syntax errors. |
|
@nmenke I did not need to use this construct and looking at the current definiton of |
@eps1lon This is extremely weird. It works in an empty project in my laptop. Was talking about class names completion from |
Okay, in my case, it doesn't work only when there are |
For example, the following does not compile, complaining:
class MyPage extends React.PureComponent<WithStyles<typeof styles>> {
static getDerivedStateFromProps(props: WithStyles<typeof styles>) {...}
render() {
...
return ...;
}
}
export default withStyles(styles)(MyPage); whereas if you take @pelotom Any ideas? 🤕 BTW, it started happening when I bumped up versions of both |
Update: |
@franklixuefei can you reproduce this in a codesandbox? I'm not able to reproduce here: https://codesandbox.io/s/2m0j23klp |
@pelotom I've been trying to repro this in codesandbox, but with no luck. It seems like codesandbox is using a version of However, if you set up your own environment in VSCode, with After some more digging, I think this is related to the latest change in Problematic code: import * as React from "react";
import { Theme } from "@material-ui/core/styles/createMuiTheme";
import withStyles, {
CSSProperties,
WithStyles
} from "@material-ui/core/styles/withStyles";
interface State {
test: string;
test2: number;
}
const styles = (theme: Theme) => ({
style1: {
position: "relative"
} as CSSProperties
});
class Test extends React.PureComponent<WithStyles<typeof styles>, State> {
constructor(props: WithStyles<typeof styles>) {
super(props);
this.state = {
test: "test",
test2: 2
};
}
static getDerivedStateFromProps(
props: WithStyles<typeof styles>,
state: State // <-- If you comment this out, the error will disappear
): Partial<State> | null {
return {
test: '123'
};
}
render() {
return null;
}
}
export default withStyles(styles)(Test); // <-- Error: Type 'typeof Test' provides no match for the signature '(props: Partial<WithTheme> & { classes: Record<"style1", string>; } & { children?: ReactNode; }, context?: any): ReactElement<any> | null'. Really not sure why this is happening... For now I'll just bump down to Any ideas will be appreciated! |
After some more digging... I found the root cause in @types/react. According to a previous commit - DefinitelyTyped/DefinitelyTyped#27417, export default function withStyles<ClassKey extends string>(
style: StyleRulesCallback<ClassKey> | StyleRules<ClassKey>,
options?: WithStylesOptions<ClassKey>,
): {
<P extends ConsistentWith<P, StyledComponentProps<ClassKey>>>(
component: React.ComponentType<P & WithStyles<ClassKey>>,
): React.ComponentType<Overwrite<P, StyledComponentProps<ClassKey>>>;
}; where we don't specify a state type in As a proof, if I change the type of import * as React from "react";
import { Theme } from "@material-ui/core/styles/createMuiTheme";
import withStyles, {
CSSProperties,
WithStyles
} from "@material-ui/core/styles/withStyles";
interface State {
test: string;
test2: number;
}
const styles = (theme: Theme) => ({
style1: {
position: "relative"
} as CSSProperties
});
class Test extends React.PureComponent<WithStyles<typeof styles>, State> {
constructor(props: WithStyles<typeof styles>) {
super(props);
this.state = {
test: "test",
test2: 2
};
}
static getDerivedStateFromProps(
props: WithStyles<typeof styles>,
state: {} // <-- changed from State to {}, and the code compiles
): Partial<State> | null {
return {
test: '123'
};
}
render() {
return null;
}
}
export default withStyles(styles)(Test); |
@franklixuefei I see, so the definition of export default function withStyles<ClassKey extends string>(
style: StyleRulesCallback<ClassKey> | StyleRules<ClassKey>,
options?: WithStylesOptions<ClassKey>,
): {
<P extends ConsistentWith<P, StyledComponentProps<ClassKey>>, S>(
component: React.ComponentType<P & WithStyles<ClassKey>, S>,
): React.ComponentType<Overwrite<P, StyledComponentProps<ClassKey>>, S>;
}; |
@pelotom I tried exactly that, but it will break the existing code, because I just filed a PR against |
@pelotom There is one more important reason why only changing So that's why I decided to just change |
@franklixuefei I see, makes sense. Good luck with the PR! |
FYI - DefinitelyTyped/DefinitelyTyped#27914 is now checked in. At least my issue is now resolved. 😄 |
@franklixuefei does that mean this can be closed? |
Great, thanks! |
WithStyles<typeof styles>
doesn't get along with with TS (no auto completion and I get type mismatch error).Expected Behavior
New Material UI Core's
WithStyles<>
should be able to receive the the type ofstyles
object somehow, for further auto-completion of classes fromthis.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
:Tried also without
createStyles
or justwithStyles(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 inwithStyles
: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
The text was updated successfully, but these errors were encountered: