Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

No, it is possible to pass a component as a prop in TypeScript. However, you need to specify the type of the component using the "React.ComponentType" interface. Here's an example:

import React from "react";

interface Props {
  component: React.ComponentType<any>;
}

const MyComponent: React.FC<Props> = ({ component: Component }) => {
  return <Component />;
};

const MyOtherComponent: React.FC = () => {
  return <div>Hello TypeScript!</div>;
};

const App: React.FC = () => {
  return <MyComponent component={MyOtherComponent} />;
};

In this example, we have defined a component called "MyComponent" that takes a prop called "component" of type "React.ComponentType<any>". We then use this prop to render the passed in component ("MyOtherComponent" in this case).