我正在使用 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>'.
Run Code Online (Sandbox Code Playgroud)
在开发和构建方面工作正常,唯一的抱怨是错误消息。我添加了一个自定义类型来尝试解决,但它不起作用。
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>'.
Run Code Online (Sandbox Code Playgroud)
const theme = …Run Code Online (Sandbox Code Playgroud) 我正在尝试解决有关滑块组件上所选颜色的打字稿错误:
<Slider
color="brown"
/>
Run Code Online (Sandbox Code Playgroud)
错误是:Type '"brown"' is not assignable to type '"primary" | "secondary" | undefined'.
我通过增强 createPalette 文件在我的主题上设置了棕色。
declare module '@mui/material/styles/createPalette' {
interface Palette {
brown: PaletteColor
}
interface PaletteOptions {
brown: PaletteColorOptions
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在我仍然遇到错误,我在文件中查找Slider.d.ts并找到了一个接口:SliderPropsColorOverrides。
export interface SliderPropsColorOverrides {}
...
color?: OverridableStringUnion<'primary' | 'secondary', SliderPropsColorOverrides>;
Run Code Online (Sandbox Code Playgroud)
我尝试将它与我的棕色合并:
declare module '@mui/material/Slider' {
interface SliderPropsColorOverrides {
darkest_blue: PaletteColorOptions
}
}
Run Code Online (Sandbox Code Playgroud)
但没有运气。要么我的 IDE (PhpStorm 2021.3) 没有编译新的打字稿代码,要么我遗漏了一些东西。