Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  193] [ 6]  / answers: 1 / hits: 14282  / 3 Years ago, wed, november 17, 2021, 12:00:00

I am setting up the base for an app with MUI v5 and TypeScript. I want to extend the MUI theme with some custom properties which add to the existing default properties.


My theme.ts config looks like that:


import { createTheme, ThemeOptions } from "@mui/material/styles";

interface IThemeOptions extends ThemeOptions {}

export const themeStyles = {
palette: {...},
breakpoints: {...},
typography: {
h1: {...},
h2: {...},
h3: {...},
h4: {...},
h5: {...}
h6: {...},
button: {...},
body1: {...},
body2: {...},
//this is the custom prop I want to add
h1Bold: {
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
fontWeight: 700,
fontSize: "3.3125rem",
lineHeight: "1.15em",
color: "#1D1D1D",
marginTop: "20px",
marginBottom: "10px",
},
},
};

const theme = createTheme(themeStyles as IThemeOptions);

export default theme;

I call the "h1Bold" property in App.tsx like that:


import React from "react";

// Mui
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";

interface IProps {}

const App: React.FC = (): JSX.Element => {
return (
<Box
className="App"
component="div"
sx={{
backgroundColor: "white",
}}
>
<Typography variant="h1Bold">
I am the App
</Typography>
</Box>
);
};

export default App;

This gives me the Error:


    No overload matches this call.
"Overload 1 of 2, '(props: { component: ElementType<any>; } & SystemProps<Theme> & { align?: "right" | "left" | "inherit" | "center" | "justify" | undefined; children?: ReactNode; ... 6 more ...; variantMapping?: Partial<...> | undefined; } & CommonProps & Omit<...>): Element', gave the following error.
Type '"h1Bold"' is not assignable to type '"button" | "caption" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "inherit" | "subtitle1" | "subtitle2" | "body1" | "body2" | "overline" | undefined'.
Overload 2 of 2, '(props: DefaultComponentProps<TypographyTypeMap<{}, "span">>): Element', gave the following error.
Type '"h1Bold"' is not assignable to type '"button" | "caption" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "inherit" | "subtitle1" | "subtitle2" | "body1" | "body2" | "overline" | undefined'."

So I understand this error that I have to update the definition somehow. How can I do that so that TypeScript understands the newly added "h1Bold" property?


My index.ts looks like this:


import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";
import { Provider } from "react-redux";
import store from "./redux/store";
import { persistStore } from "redux-persist";
import { PersistGate } from "redux-persist/integration/react";
import { ThemeProvider } from "@mui/material/styles";
//this is the theme created with createTheme()
import theme from "./config/theme/theme";

import App from "./App";
import "./index.css";

// Redux-Persist
let persistor = persistStore(store);

ReactDOM.render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<BrowserRouter>
<App />
</BrowserRouter>
</PersistGate>
</Provider>
</ThemeProvider>
</React.StrictMode>,
document.getElementById("root")
);

Would be great if someone could give me some insight or has a solution how to extend and read from the MUI theme more easily with having TypeScript in use.


Cheers


More From » reactjs

 Answers
23

To update Typography variant in Typescript, use the following code:


declare module '@mui/material/Typography' {
interface TypographyPropsVariantOverrides {
h1Bold: true;
}
}

The properties of the interface above are then merged with the existing variants. Now the variant of Typography is:


"h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "subtitle1" | "subtitle2" | "body1" | "body2" | "caption" | "button" | "overline" | "inherit" | "h1Bold"

You can now use the new variant without being complained by Typescript:


<Typography variant='h1Bold'

Live Demo


Codesandbox


[#672] Monday, November 8, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
albert questions
;