5

I am creating custom typography. But while using I am getting below error.

I am following below document

https://mui.com/material-ui/api/typography/

here is my code https://codesandbox.io/s/fragrant-mountain-eukirg

import * as React from "react";
import Typography, { TypographyProps } from "@mui/material/Typography";
import { styled } from "@mui/material/styles";

const LabelXs = styled(Typography)<TypographyProps>(({ fontWeight }) => {
  return {
    fontSize: 15
  };
});

export default LabelXs;

I am using like this

 <LabelXs component={"div"}>Div element</LabelXs>

don't why it is showing error ?

enter image description here

  [![Property 'component' does not exist on type 'IntrinsicAttributes & SystemProps<Theme> & { align?: "right" | "left" | "inherit" | "center" | "justify" | undefined; children?: ReactNode; ... 6 more ...; variantMapping?: Partial<...> | undefined; } & CommonProps & Omit<...> & MUIStyledCommonProps<...>'.ts(2322)][1]][1]

any suggestion ?

enter image description here component props is present why you are saying it is not present https://mui.com/material-ui/api/typography/

2
  • If you go to Typography.d.ts file you will see that there is no component prop. I do not why. You could edit that file if it makes any sense. Commented Dec 26, 2022 at 5:55
  • 1
    Have you tried the options in the MUI docs? mui.com/material-ui/guides/composition/#with-typescript Commented Dec 26, 2022 at 6:48

2 Answers 2

3

Component seems not to be present in TypographyProps. Therefore, inspired by @SteveGomez, you can do something like: <TypographyProps & {component: React.ElementType}>

import * as React from "react";
import Typography, { TypographyProps } from "@mui/material/Typography";
import { styled } from "@mui/material/styles";

const LabelXs = styled(Typography)<TypographyProps & {component: React.ElementType}>(({ fontWeight }) => {
  return {
    fontSize: 15
  };
});

export default LabelXs;

EDIT: You could also use {component: keyof JSX.IntrinsicElements}.

Sign up to request clarification or add additional context in comments.

4 Comments

🙌 You don't need component: string -- React.ElementType should be sufficient. Or even: component: keyof JSX.IntrinsicElements
@SteveGomez thanks for answering , But I saw there is component props in documentation . see my updated question
Unfortunatelly base component (Typography) will not inherit props of a "component" like it does without styled 😔 i.e const CustomButton = styled(Button) and <CustomButton component={"a"} target="..."> will throw error as target is not a prop of CustomButton
2

To be able to use the component prop, the type of the props should be used with type arguments. Just use React.ElementType as mentioned in the docs.

const LabelXs = styled(Typography)<
  TypographyProps & { component: React.ElementType }
>(({ fontWeight }) => {
  return {
    fontSize: 15
  };
});

Now component accepts any React Custom Component and HTML element.

Docs

1 Comment

But I saw there is component props in documentation . see my updated question –

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.