我正在使用 TypeScript 使用 Material UI,并希望向我的主题添加自定义颜色。一切工作正常,除了 VSCode linter 向我显示下一条消息。
Type '{ tan: string; lightRed: string; red: string; offBlack: string; white: string; }' is not assignable to type 'Partial<CommonColors>'.
Object literal may only specify known properties, and 'tan' does not exist in type 'Partial<CommonColors>'.
在开发和构建方面工作正常,唯一的抱怨是错误消息。我添加了一个自定义类型来尝试解决,但它不起作用。
const theme = createTheme({
palette: {
common: {
tan,
lightRed,
red,
offBlack,
white,
},
},
});
import {
PaletteOptions,
CommonColors,
} from '@material-ui/core/styles/createPalette';
interface CustomColors extends CommonColors {
tan: string?;
lightRed: string;
red: string;
offBlack: string;
}
declare module '@material-ui/core/styles/createPalette' {
export interface PaletteOptions {
common: CustomColors;
}
}
我添加到tsconfig.json文件。 tan、red 和其余值被声明为字符串。关于如何解决这个问题有任何线索吗?提前致谢。