我正在尝试使用Material UI扩展Theme
,但是出现错误,提示我扩展不正确,错误信息如下:
Property 'layout' is missing in type 'Palette' but required in type 'IPaletteOptions'.
以下是我的内容:
// src/theme/theme.d.ts
import { Theme } from "@material-ui/core/styles";
import {
IPaletteOptions,
PaletteOptions
} from "@material-ui/core/styles/createPalette";
type TLayout = {
primary: string;
secondary: string;
};
declare module "@material-ui/core/styles/createPalette" {
export interface IPaletteOptions extends PaletteOptions {
layout: TLayout;
}
}
declare module "@material-ui/core/styles" {
export interface ITheme extends Theme {
palette: IPaletteOptions;
}
}
// src/theme/index.ts
import { createMuiTheme, ITheme } from "@material-ui/core/styles";
import { IPaletteOptions } from "@material-ui/core/styles/createPalette";
export const palette = {
layout: {
primary: "#ffffff",
secondary: "#e4e4e4"
}
} as IPaletteOptions;
export default createMuiTheme({ palette }) as ITheme;
// src/App.tsx
import React from "react";
import { ThemeProvider, ITheme } from "@material-ui/core";
import theme from "./theme";
import Component from "./Component";
export default function App() {
return (
<ThemeProvider<ITheme> theme={theme}>
<Component />
</ThemeProvider>
);
}
import { useTheme } from "@material-ui/core";
import React from "react";
export default React.memo(() => {
const theme = useTheme(); // <-- this should be of type `ITheme` automatically
// If I do useTheme<ITheme>(), it shows property 'layout', but
// I shouldn't have to do that, because of the type casting that
// I do in App.tsx, where I import and use 'theme' from src/theme/index.ts
return <div>the layout primary color is {theme.palette.layout.primary}</div>;
});
我在这里做错了什么?我想在整个应用程序中使用 ITheme
,它将扩展基本的Theme
以及我添加的内容。
declare module "@mui/material/styles/createPalette"
中有特殊路径来覆盖这个问题,这非常有帮助。 - Narigo