question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createMuiTheme custom variables not recognized in Typescript

See original GitHub issue

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',
    }
}})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
marjan2kcommented, Dec 4, 2018

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

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"
    }
  }
});
3reactions
eps1loncommented, Dec 4, 2018
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

createMuiTheme custom variables not recognized in Typescript
I followed the reference to add custom variables as I needed more color palettes as opposed to just primary and secondary. Typescript keeps ......
Read more >
How to use Material UI custom theme in React with Typescript
import { createMuiTheme } from '@material-ui/core/styles'; declare module ... You can declare your custom variables in .d.ts file.
Read more >
Extend Material-UI theme in TypeScript - In Plain English
Object literal may only specify known properties, and 'success' does not exist in type 'PaletteOptions'. It means that because Material-UI ...
Read more >
Dark mode - Material UI - MUI
If you have a custom palette, make sure that you have the correct values based on the mode . The next section explains...
Read more >
Update! Creating a Custom Material-UI Theme
We found it too difficult to create a color palette that was pleasing to ... When creating the custom theme object, I created...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found