在为 material-ui
创建我的主题时,我添加了两个新的调色板选项,使我拥有更好的浅色和深色范围。我已经扩展了 Theme
类型来指示此操作。
import {Theme} from "material-ui/styles";
import {Palette} from "material-ui/styles/createPalette";
export interface ExtendedTheme extends Theme {
palette: ExtendedPalette
}
export interface ExtendedPalette extends Palette {
light: Color,
dark: Color,
}
当我尝试在WithStyles
渲染助手中使用这些附加选项时,会出现问题。
const styles = (theme : ExtendedTheme) => ({ root: {color: theme.light['100'] }});
export interface MyProps {classes: {[index: string] : string}};
const MyComponent = (props : MyProps) => {...};
// Type ExtendedTheme is not assignable to Theme
export default withStyles(styles : StyleRulesCallback)(MyComponent);
从功能上讲,我的纯JavaScript代码可以正常工作,但由于类型不同,它会抛出错误。material-ui的类型定义期望样式回调函数的唯一参数是Theme
类型:
export type StyleRulesCallback<ClassKey extends string = string> = (theme: Theme) => StyleRules<ClassKey>;
我认为通过扩展接口的方式可以实现多态,使ExtendedTheme
实现Theme
import { Theme } from "@material-ui/core/styles/createMuiTheme";
- thisismydesign