我正在尝试在 createMuiTheme 中添加一个新属性。但 Typescript 不让我这样做。
我按照这里的说明进行操作:https://next.material-ui.com/guides/typescript/#customization-of-theme https://next.material-ui.com/guides/typescript/#customization-of-theme
我创建了一个 .ts 文件,将其放置在其中:
import * as React from 'react';
declare module '@material-ui/core/styles' {
interface TypeBackground {
darker: string;
}
}
并且,这是我创建 Material-UI 主题的地方:
import {createMuiTheme} from "@material-ui/core";
export const darkTheme = createMuiTheme({
palette: {
background: {
darker: '#fafa',
},
mode: 'dark'
},
})
但我收到此错误:
TS2322: Type '{ darker: string; }' is not assignable to type 'Partial<TypeBackground>'. Object literal may only specify known properties, and 'darker' does not exist in type 'Partial<TypeBackground>'
即便如此,我还是尝试了material-ui团队的示例,它可以工作,但不是我的代码。我找不到原因。我可能应该错过一些东西。
我正在使用接口“TypeBackground”,因为它的命名类似于material-ui 中的createPalette.d.ts 文件中的名称。
export interface TypeBackground {
default: string;
paper: string;
}
所以我也尝试做同样的事情。我认为这与向调色板属性添加道具相同。
Thanks!