React: Generic Type
type ExampleComponentProps<T extends React.ReactElement> = { children: T; }; const ExampleComponent = <T extends React.ReactElement>({ children }: ExampleComponentProps<T>): React.ReactElement => { if ('onClick' in children.props) { console.log("children has an onClick prop"); } return <div>{children}</div>; };
・By using T extends React.ReactElement, the children prop of ExampleProps can accept any React element as its value. ・The component can receive the props of the children (ie: onClick).