Table of contents

Material-UI makeStyles function causes compilation error: No overload matches this call

Javascript Sep 24, 2020 Viewed 3K Comments 0

Issue

In the React + TypeScript + Material-UI project, using the makeStyles method, the code is as follows:

import { makeStyles, Theme } from '@material-ui/core/styles';
export default class Admin extends React.Component<{}, {}> {
    render() {
        let styles = (theme: Theme) => ({
            wrapper: {
                position: "relative",
                top: "0",
                height: "100vh"
            }
        });
        let useStyles = makeStyles(styles);
        return <div>Admin</div>;
    }
}

I get the error message saying:

No overload matches this call.
  Overload 1 of 2, '(style: Styles<Theme, {}, "wrapper">, options?: Pick<WithStylesOptions<Theme>, "flip" | "element" | "defaultTheme" | "name" | "media" | "meta" | "index" | "link" | "generateId" | "classNamePrefix"> | undefined): (props?: any) => Record<...>', gave the following error.
    Argument of type '(theme: Theme) => { wrapper: { position: string; top: string; height: string; }; }' is not assignable to parameter of type 'Styles<Theme, {}, "wrapper">'.
      Type '(theme: Theme) => { wrapper: { position: string; top: string; height: string; }; }' is not assignable to type 'StyleRulesCallback<Theme, {}, "wrapper">'.
        Call signature return types '{ wrapper: { position: string; top: string; height: string; }; }' and 'Record<"wrapper", CSSProperties | CreateCSSProperties<{}> | PropsFunc<{}, CreateCSSProperties<{}>>>' are incompatible.
          The types of 'wrapper' are incompatible between these types.
            Type '{ position: string; top: string; height: string; }' is not assignable to type 'CSSProperties | CreateCSSProperties<{}> | PropsFunc<{}, CreateCSSProperties<{}>>'.
              Type '{ position: string; top: string; height: string; }' is not assignable to type 'CreateCSSProperties<{}>'.
                Types of property 'position' are incompatible.
                  Type 'string' is not assignable to type '"relative" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "fixed" | "-webkit-sticky" | "absolute" | "static" | "sticky" | PropsFunc<{}, "relative" | "-moz-initial" | ... 9 more ... | undefined> | undefined'.
  Overload 2 of 2, '(styles: Styles<Theme, {}, "wrapper">, options?: Pick<WithStylesOptions<Theme>, "flip" | "element" | "defaultTheme" | "name" | "media" | "meta" | "index" | "link" | "generateId" | "classNamePrefix"> | undefined): (props: {}) => Record<...>', gave the following error.
    Argument of type '(theme: Theme) => { wrapper: { position: string; top: string; height: string; }; }' is not assignable to parameter of type 'Styles<Theme, {}, "wrapper">'.
      Type '(theme: Theme) => { wrapper: { position: string; top: string; height: string; }; }' is not assignable to type 'StyleRulesCallback<Theme, {}, "wrapper">'.  TS2769

    23 |             }
    24 |         });
  > 25 |         let useStyles = makeStyles(styles);
       |                                    ^
    26 |         return <div>Admin</div>;
    27 |     }
    28 | }

Solution

The official document says, makeStyles can be used in two ways.

// 1
const useStyles = makeStyles((theme: Theme) => createStyles({
  root: {
    backgroundColor: theme.color.red,
  },
}));

// 2
const useStyles = makeStyles({
  root: {
    backgroundColor: 'red',
    color: props => props.color,
  },
});

This problem is solved with method 1, add the createStyles method. Such as:

import { makeStyles, Theme, createStyles } from '@material-ui/core/styles'
export default class Admin extends React.Component<{}, {}> {
    render() {
        let styles = (theme: Theme) => createStyles({
            wrapper: {
                position: "relative",
                top: "0",
                height: "100vh"
            }
        });
        let useStyles = makeStyles(styles);
        return <div>Admin</div>;
    }
}
Updated Sep 24, 2020