Shamoon
Shamoon

Reputation: 43491

What Type should I use for TypeScript props with children?

I'm doing:

export default function PageTitle(props: ReactElement) {
  return (
    <Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
  )
}

But I get a complaint:

Type error: Property 'children' does not exist on type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)>) | (new (props: any) => Component<any, any, any>)>'

What's the right type to use?

Upvotes: 2

Views: 161

Answers (1)

A.R.SEIF
A.R.SEIF

Reputation: 873

I will write it like this

   type Props = {
      children: string;
    };

     const PageTitle : React.FC<Props> = (props) => {
      return (
        <Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
      )
    }

    export default PageTitle;

or like this

type Props = {
  children: string;
};

 const PageTitle = ({children, ...props}: Props) => {
  return (
    <Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{children}</Typography>
  )
}

export default PageTitle;

Upvotes: 2

Related Questions