簡體   English   中英

MUI v5 - 擴展 TypeScript 中的排版變體創建錯誤“沒有重載匹配此調用”

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

我正在為具有 MUI v5 和 TypeScript 的應用程序設置基礎。 我想使用一些添加到現有默認屬性的自定義屬性來擴展 MUI 主題。

我的 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;

我這樣稱呼 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;

這給了我錯誤:

    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'."

所以我理解這個錯誤,我必須以某種方式更新定義。 我該怎么做才能讓 TypeScript 理解新添加的“h1Bold”屬性?

我的 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")
);

如果有人能給我一些見解或有一個解決方案如何通過使用 TypeScript 來更輕松地擴展和閱讀 MUI 主題,那就太好了。

干杯

要更新 Typescript 中的Typography變體,請使用以下代碼:

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

然后將上述接口的屬性與現有變體合並 現在Typographyvariant是:

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

您現在可以使用新變體而不會被 Typescript 抱怨:

<Typography variant='h1Bold'

現場演示

Codesandbox 演示

根據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