While developing my theme using material-ui
, I decided to introduce two new palette options that would offer a wider range of light and dark shades. To achieve this, I extended the Theme
type with the necessary modifications:
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,
}
The complication arises when attempting to apply these additional options within the WithStyles
render helper:
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);
Despite the functional success of my code in pure javascript, it encounters an error due to the mismatched types. The material-ui typings are set up to expect only the Theme
type as the argument for the style callback function:
export type StyleRulesCallback<ClassKey extends string = string> = (theme: Theme) => StyleRules<ClassKey>;
I initially believed that extending an interface would allow for polymorphic implementation, where ExtendedTheme
would seamlessly incorporate Theme
.