简体   繁体   中英

DefaultTheme in Material-UI causes `Invalid module name in augmentation` error

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?

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 {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM