我正在做:
export default function PageTitle(props: ReactElement) {
return (
<Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
)
}
但我收到投诉:
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>)>'
什么是正确的类型?
最佳答案
我会这样写
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;
或者像这样
type Props = {
children: string;
};
const PageTitle = ({children, ...props}: Props) => {
return (
<Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{children}</Typography>
)
}
export default PageTitle;
关于reactjs - 对于 child 的 TypeScript Prop ,我应该使用什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64508053/