Home > Software engineering >  defaultTheme in materialUI causes `Invalid module name in augmentation` error
defaultTheme in materialUI causes `Invalid module name in augmentation` error

Time:10-12

As described in the migration docs (v4 to v5) I've added this to my theme:

import { createTheme, Theme } from '@mui/material/styles'
import { grey } from '@mui/material/colors'

declare module '@mui/styles/defaultTheme' { // <-- ts error
  // eslint-disable-next-line @typescript-eslint/no-empty-interface
  interface DefaultTheme extends Theme {}
}

const { palette } = createTheme()
const { augmentColor } = palette

// Create a theme instance.
export const theme: Theme = createTheme({
  palette: {
    neutral: augmentColor({ color: { main: grey[400] } }),
  }
})

But I get the ts error Invalid module name in augmentation, module '@mui/styles/defaultTheme' cannot be found.ts(2664)

What am I doing wrong?

CodePudding user response:

I don't know why, but importing the module for side-effects fix it:

import { Theme } from "@mui/material/styles";
import "@mui/styles";

declare module "@mui/styles/defaultTheme" {
  // eslint-disable-next-line @typescript-eslint/no-empty-interface (remove this line if you don't have the rule enabled)
  interface DefaultTheme extends Theme {}
}
  • Related