Material-ui: createMuiTheme custom variables not recognized in Typescript

Created on 4 Dec 2018  路  5Comments  路  Source: mui-org/material-ui

Reference: https://material-ui.com/customization/themes/#custom-variables

I followed the reference to add custom variables as I needed more color palettes as opposed to just primary and secondary.

Typescript keeps complaining blue:聽{聽light:聽string;聽main:聽string;聽dark:聽string;聽contrastText:聽string;聽};'聽is聽not聽assignable聽to聽parameter聽of聽type聽'ThemeOptions'

export default createMuiTheme({
  palette: {
    primary: {
      light: '#FFB644',
      main: '#FF8500',
      dark: '#C55600',
      contrastText: '#FFFFFF',
    },
    secondary: {
      light: '#96F87D',
      main: '#62C44D',
      dark: '#2A931C',
      contrastText: '#FFFFFF',
    },
    blue: {
      light: '#72C3FF',
      main: '#3393DA',
      dark: '#0066A8',
      contrastText: '#FFFFFF',
    }
}})
docs typescript

Most helpful comment

A small change I made to keep it more general is use interfaces PaletteColor and PaletteColorOptions.

```tsx
import createMuiTheme from "@material-ui/core/styles/createMuiTheme";
import { PaletteColor, PaletteColorOptions } from "@material-ui/core/styles/createPalette";

declare module "@material-ui/core/styles/createPalette" {
interface Palette {
blue: PaletteColor;
}

interface PaletteOptions {
blue: PaletteColorOptions;
}
}

export default createMuiTheme({
palette: {
blue: {
main: "blue"
}
}
});

All 5 comments

I believe you can solve this with module augmentation:

import { Theme } from '@material-ui/styles';
declare module "@material-ui/styles" {
    interface Theme {
        blue: Theme['palette']['primary'];
    }
}

That way you should also have the typings of your Theme available when using createStyles. However this would mean the every instance of Theme would be assumed to have the blue color. Even if you have multiple MuiThemeProvider and different themes.

Please let me know if this worked. A working example with all caveats should be added to the docs.

Hey @eps1lon,

Thanks for the quick response. In our case, we actually want every instance of Theme to have blue.

I think I'm still missing finer details.

image

import { Palette, PaletteOptions } from "@material-ui/core/styles/createPalette";
import { createMuiTheme } from "@material-ui/core/styles";

declare module "@material-ui/core/styles/createPalette" {
  interface Palette {
    blue: Palette["primary"];
  }

  interface PaletteOptions {
    blue: PaletteOptions["primary"];
  }
}

export default createMuiTheme({
  palette: {
    blue: {
      main: "blue"
    }
  }
});

That should do the trick.

Yes that did it thanks!

A small change I made to keep it more general is use interfaces PaletteColor and PaletteColorOptions.

```tsx
import createMuiTheme from "@material-ui/core/styles/createMuiTheme";
import { PaletteColor, PaletteColorOptions } from "@material-ui/core/styles/createPalette";

declare module "@material-ui/core/styles/createPalette" {
interface Palette {
blue: PaletteColor;
}

interface PaletteOptions {
blue: PaletteColorOptions;
}
}

export default createMuiTheme({
palette: {
blue: {
main: "blue"
}
}
});

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sys13 picture sys13  路  3Comments

pola88 picture pola88  路  3Comments

ghost picture ghost  路  3Comments

ericraffin picture ericraffin  路  3Comments

revskill10 picture revskill10  路  3Comments