简体   繁体   English

MUI v5 - 扩展 TypeScript 中的排版变体创建错误“没有重载匹配此调用”

[英]MUI v5 - Extending Typography variant in TypeScript creates error "No overload matches this call"

I am setting up the base for an app with MUI v5 and TypeScript.我正在为具有 MUI v5 和 TypeScript 的应用程序设置基础。 I want to extend the MUI theme with some custom properties which add to the existing default properties.我想使用一些添加到现有默认属性的自定义属性来扩展 MUI 主题。

My theme.ts config looks like that:我的 theme.ts 配置如下所示:

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:我这样称呼 App.tsx 中的“h1Bold”属性:

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?我该怎么做才能让 TypeScript 理解新添加的“h1Bold”属性?

My index.ts looks like this:我的 index.ts 看起来像这样:

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.如果有人能给我一些见解或有一个解决方案如何通过使用 TypeScript 来更轻松地扩展和阅读 MUI 主题,那就太好了。

Cheers干杯

To update Typography variant in Typescript, use the following code:要更新 Typescript 中的Typography变体,请使用以下代码:

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:现在Typographyvariant是:

"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:您现在可以使用新变体而不会被 Typescript 抱怨:

<Typography variant='h1Bold'

Live Demo现场演示

Codesandbox 演示

In MUI v5 there are a couple extra properties you need to declare in TypeScript, per the MUI docs .根据MUI 文档,在 MUI v5 中,您需要在 TypeScript 中声明几个额外的属性。

declare module "@mui/material/styles" {
  interface TypographyVariants {
    h1Bold: React.CSSProperties;
  }

  // allow configuration using `createTheme`
  interface TypographyVariantsOptions {
    h1Bold?: React.CSSProperties;
  }
}

// Update the Typography's variant prop options
declare module "@mui/material/Typography" {
  interface TypographyPropsVariantOverrides {
    h1Bold: true;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM