gpt4 book ai didi

javascript - 如何使用 Typescript 扩展 Material-UI 主题?

转载 作者:行者123 更新时间:2023-12-03 07:20:18 25 4
gpt4 key购买 nike

Typescript 总是提示调色板中缺少某些属性。如果我添加 //@ts-ignore,我的应用程序就可以正常工作,但我显然想避免这种情况。我是 Typescript 的新手,这就是我尝试过的。

import createMuiTheme, { ThemeOptions, Theme } from '@material-ui/core/styles/createMuiTheme';
import { PaletteOptions } from '@material-ui/core/styles/createPalette';

interface IPaletteOptions extends PaletteOptions {
chip: {
color: string,
expandIcon: {
background: string,
color: string,
},
},
}
interface ITheme extends Theme {
palette: IPaletteOptions,
}

const theme: ITheme = createMuiTheme({
typography: {
fontWeightMedium: 600,
fontFamily: ['Open Sans', 'Arial', 'sans-serif'].join(','),
},
palette: {
primary: {
main: '#43C099',
},
secondary: {
main: '#7AF3CA',
},
chip: {
color: '#C2C3C6',
expandIcon: {
background: '#808183',
color: '#FFFFFF',
},
},
},
} as ThemeOptions);

这会引发错误,
Type 'Theme' is not assignable to type 'ITheme'.
Types of property 'palette' are incompatible.
Property 'chip' is missing in type 'Palette' but required in type 'IPaletteOptions

这对我来说是一个令人困惑的错误,因为我没有使用类型 Palette任何地方。

如何在此处正确扩展调色板?

最佳答案

这可以通过 Module Augmentation 更轻松地解决。 :
MUI v5material-ui.d.ts

import { PaletteOptions } from "@mui/material/styles/createPalette";

declare module "@mui/material/styles/createPalette" {
export interface PaletteOptions {
chip: {
color: string;
expandIcon: {
background: string;
color: string;
};
};
}
}
MUI v4 material-ui.d.ts
import { PaletteOptions } from "@material-ui/core/styles/createPalette";

declare module "@material-ui/core/styles/createPalette" {
export interface PaletteOptions {
chip: {
color: string;
expandIcon: {
background: string;
color: string;
};
};
}
}

关于javascript - 如何使用 Typescript 扩展 Material-UI 主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61120760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com