在Material UI主题中的“Typography”中添加自定义新属性“tab”时出现Typescript类型错误

sdnqo3pr  于 2023-05-30  发布在  TypeScript
关注(0)|答案(1)|浏览(230)

目前,使用

  1. "react": "^17.0.2",
  2. @material-ui/core": "^4.12.3",
  3. "@material-ui/icons": "^4.11.2",
  4. "@material-ui/styles": "^4.11.4",

在Material UI主题中的“Typography”内添加自定义新属性“tab”时出现Typescript类型错误
错误:属性“tab”在类型“Typography”上不存在
它在theme.tsx文件中工作正常
主题.tsx文件

  1. declare module "@material-ui/core/styles/createTypography" {
  2. interface TypographyOptions {
  3. tab?: {
  4. fontFamily?: string;
  5. textTransform?: string;
  6. fontWeight?: number;
  7. fontSize?: string;
  8. };
  9. }
  10. }
  11. const theme = createTheme({
  12. typography: {
  13. tab: {
  14. fontFamily: "Raleway",
  15. textTransform: "none",
  16. fontWeight: 700,
  17. fontSize: "1rem",
  18. },
  19. },
  20. });

在另一个typescript组件上,我得到属性'tab'错误类型'Typography'上不存在属性'tab'

  1. const useStyles = makeStyles((theme) => ({
  2. tab: {
  3. ...theme.typography.tab, // error: Property 'tab' does not exist on type 'Typography'
  4. minWidth: 10,
  5. marginLeft: "25px",
  6. },
  7. }));

那么如何获得新的自定义主题 prop 呢?

polkgigr

polkgigr1#

正确的主题设置。

  1. import { createTheme } from "@material-ui/core";
  2. const arcBlue = "#0B72B9";
  3. const arcOrange = "#FF8C00";
  4. declare module "@material-ui/core/styles/createPalette" {
  5. interface CommonColors {
  6. blue: string;
  7. orange: string;
  8. }
  9. }
  10. declare module "@material-ui/core/styles/createTypography" {
  11. interface Typography {
  12. tab: TypographyStyleOptions | undefined;
  13. }
  14. interface TypographyOptions {
  15. tab: TypographyStyleOptions | undefined;
  16. }
  17. }
  18. const theme = createTheme({
  19. palette: {
  20. primary: {
  21. main: `${arcBlue}`,
  22. },
  23. secondary: {
  24. main: `${arcOrange}`,
  25. },
  26. common: {
  27. blue: `${arcBlue}`,
  28. orange: `${arcOrange}`,
  29. },
  30. },
  31. typography: {
  32. tab: {
  33. fontFamily: "Raleway",
  34. textTransform: "none",
  35. fontWeight: 700,
  36. fontSize: "1rem",
  37. },
  38. },
  39. });
  40. export default theme;
展开查看全部

相关问题