React.ComponentType

React.ComponentType<P> is the type for either a class component (React.ComponentClass<P>) or function component (React.FunctionComponent<P> aka React.FC<P>) which takes props P.

(FYI, React.ReactNode and React.ReactElement are the types for the JSX returned by the component - not for a callable component)

Typing Comp's Props

You do not want to use <unknown> in your Props type. You want to declare that the component requires only the props which you are providing when you call React.createElement(Comp) (you call also use JSX and return <Comp/>). In this case you are providing no props, so it would be React.ComponentType<{}> or just React.ComponentType since {} is the default for P.

interface Props {
  Comp: React.ComponentType;
}

const MyComp: React.FC<Props> = ({ Comp }) => {
  return <Comp />
}

with some props:

interface Props {
  Comp: React.ComponentType<{someKey: string}>;
}

const MyComp: React.FC<Props> = ({ Comp }) => {
  return <Comp someKey="someValue" />
}

You will get an error if you call Comp without providing someKey, which is good! You don't get that error when calling React.createElement(Comp) because for some reason the props argument is optional. So in my opinion the JSX approach is better.

Answer from Linda Paiste on Stack Overflow
🌐
React TypeScript Cheatsheets
react-typescript-cheatsheet.netlify.app › typing component props
Typing Component Props | React TypeScript Cheatsheets
Relevant for components that accept other React components as props. export declare interface AppProps { children?: React.ReactNode; // best, accepts everything React can render childrenElement: React.JSX.Element; // A single React element style?: React.CSSProperties; // to pass through style props onChange?: React.FormEventHandler<HTMLInputElement>; // form events! the generic parameter is the type of event.target // more info: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase/#wrappingmirroring props: Props & React.ComponentPropsWithoutRef<"button">; // to impersonate all the props of a button element and explicitly not forwarding its ref props2: Props & React.ComponentPropsWithRef<MyButtonWithForwardRef>; // to impersonate all the props of MyButtonForwardedRef and explicitly forwarding its ref }
Top answer
1 of 3
52

React.ComponentType

React.ComponentType<P> is the type for either a class component (React.ComponentClass<P>) or function component (React.FunctionComponent<P> aka React.FC<P>) which takes props P.

(FYI, React.ReactNode and React.ReactElement are the types for the JSX returned by the component - not for a callable component)

Typing Comp's Props

You do not want to use <unknown> in your Props type. You want to declare that the component requires only the props which you are providing when you call React.createElement(Comp) (you call also use JSX and return <Comp/>). In this case you are providing no props, so it would be React.ComponentType<{}> or just React.ComponentType since {} is the default for P.

interface Props {
  Comp: React.ComponentType;
}

const MyComp: React.FC<Props> = ({ Comp }) => {
  return <Comp />
}

with some props:

interface Props {
  Comp: React.ComponentType<{someKey: string}>;
}

const MyComp: React.FC<Props> = ({ Comp }) => {
  return <Comp someKey="someValue" />
}

You will get an error if you call Comp without providing someKey, which is good! You don't get that error when calling React.createElement(Comp) because for some reason the props argument is optional. So in my opinion the JSX approach is better.

2 of 3
-1

What I use and see in many projects is this:

interface ComponentProps {
    myComponentAsProp: () => React.ReactNode
}

const HomePage: React.FC<ComponentProps> = (props) => {
  return (
    <div>
      <h3>{props.myComponentAsProp()}</h3>
    </div>
  );
};

To render it is like a function call.

Discussions

Typescript React, best way to define component with props?
Am I the only one who likes to use function keyword in top-level functions? More on reddit.com
🌐 r/reactjs
31
7
July 18, 2023
reactjs - TypeScript React: Access component property types - Stack Overflow
If you're using this with a generic, the generic would have to look like this: T extends React.FC | React.ComponentClass and then you will be able to do this: type PropType = React.ComponentProps; 2019-10-02T19:40:37.927Z+00:00 ... You can ask yourself why I'm taking typeof from ... More on stackoverflow.com
🌐 stackoverflow.com
reactjs - React component type in TypeScript - Stack Overflow
What is the correct way to describe the type of a react component in TypeScript? Say we have a function which returns a react component. The function: const getTabContent: () => ReactElement = ... More on stackoverflow.com
🌐 stackoverflow.com
React TypeScript HoC - passing Component as the prop
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
March 14, 2018
🌐
Total TypeScript
totaltypescript.com › react-component-props-type-helper
ComponentProps: React's Most Useful Type Helper | Total TypeScript
May 29, 2023 - Refs in React let you access and interact with the properties of an element. Often, it's used with form elements like inputs and buttons to extract their values or set their properties. The ComponentPropsWithRef does exactly what it says - provide the component props with its associated ref. ... In the example above, the InputProps type extracts the props of the input element, including the associated ref. Want more TypeScript knowledge?
🌐
Akhilaariyachandra
akhilaariyachandra.com › home › blog › get the type of a prop in a react component in typescript
Get the type of a prop in a React component in TypeScript | Akhila Ariyachandra
March 3, 2023 - import CustomComponent from "component-library"; import type { ComponentProps } from "react"; type PropType = ComponentProps<typeof CustomComponent>["propName"]; While upgrading this site to Next.js 13.2, I enabled the new Statically Typed Links.
Top answer
1 of 5
574

2019: noticed all answers above are quite outdated so here is a fresh one.


Lookup type

With newer TS versions you can use lookup types.

type ViewProps = View['props']

Despite being very convenient, that will only work with class components.


React.ComponentProps

The React typedefs ship with an utility to extract the type of the props from any component.

type ViewProps = React.ComponentProps<typeof View>

type InputProps = React.ComponentProps<'input'>

This is a bit more verbose, but unlike the type lookup solution:

  • the developer intent is more clear
  • this will work with BOTH functional components and class components

All this makes this solution the most future-proof one: if you decide to migrate from classes to hooks, you won't need to refactor any client code.

2 of 5
41

Starting with TypeScript 2.8, you can use conditional types, e.g. given:

interface MyComponentProps { bar: string; }
declare const MyComponent: React.Component<MyComponentProps>;

interface MyComponentClassProps { bar: string; }
declare const MyComponentClass: React.ComponentClass<MyComponentClassProps>;

interface MyStatelessComponentProps { bar: string; }
declare const MyStatelessComponent: React.StatelessComponent<MyStatelessComponentProps>;

We can define these helpers:

type GetComponentProps<T> = T extends React.ComponentType<infer P> | React.Component<infer P> ? P : never

And use them like so:

// $ExpectType MyComponentProps
type MyComponentPropsExtracted = GetComponentProps<typeof MyComponent>

// $ExpectType MyComponentClassProps
type MyComponentClassPropsExtracted = GetComponentProps<typeof MyComponentClass>

// $ExpectType MyStatelessComponentProps
type MyStatelessComponentPropsExtracted = GetComponentProps<typeof MyStatelessComponent>

Update 2018-12-31: this is now available in the official React typings via React.ComponentProps.

🌐
Total TypeScript
totaltypescript.com › concepts › react-componentprops-type-helper
React `ComponentProps<T>` Type Helper | Total TypeScript
import ThirdPartyComponent from "third-party"; type CustomProps { foo: string; bar: number; } const MyComponent = ({ foo, bar, ...props }: CustomProps & React.ComponentProps< typeof ThirdPartyComponent >) => { // use foo and bar here as needed return <ThirdPartyComponent {...props} />; };
Find elsewhere
🌐
Steve Kinney
stevekinney.com › courses › react with typescript › complete guide to react component props with typescript
Complete Guide to React Component Props with TypeScript | React with TypeScript | Steve Kinney
3 weeks ago - These helper types shine when combined to create expressive, reusable prop patterns: // A card component that needs children and refs interface CardBaseProps { title: string; footer?: ReactNode; } type CardProps = PropsWithChildren<CardBaseProps> & RefAttributes<HTMLDivElement>; const Card = forwardRef<HTMLDivElement, CardProps>( ({ title, footer, children }, ref) => { return ( <div ref={ref} className="card"> <header>{title}</header> <main>{children}</main> {footer && <footer>{footer}</footer>} </div> ); } ); // A form field that explicitly has no children type FieldProps = PropsWithoutChildr
🌐
Total TypeScript
totaltypescript.com › pass-component-as-prop-react
How to Pass a Component as a Prop in React | Total TypeScript
June 7, 2024 - Note that we're not using React.ReactElement or JSX.Element. I cover why in this article. # The second method is, instead of passing in JSX as a prop, we pass in an entire component as a prop. Some definitions here. JSX is the thing a component returns. <Wrapper /> is JSX. Wrapper is the component. The simplest way to type this in TypeScript is by using React.ComponentType:
🌐
DEV Community
dev.to › typescripttv › typing-react-props-in-typescript-5hal
Typing React Props in TypeScript - DEV Community
July 14, 2022 - React.FC specifies a function component and lets us also assign a type variable. It uses PropsWithChildren behind the scenes, so we don't have to worry about connecting our Props with it: import React from 'react'; export interface Props { heading: string; } const PostPreview: React.FC<Props> = (props) => { return ( <div> <h1>{props.heading}</h1> {props.children} </div> ); }; export default PostPreview; Thanks to the use of React.FC, the TypeScript compiler now knows that our PostPreview constant is a React component.
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
Defining Props in React Function Component with Typescript | Pluralsight
August 25, 2020 - Learn how to define props for function components in React projects with Typescript. We cover several different methods, including destructuring and React.FC. ... This guide will help you properly define the props entering your function components. You'll also learn when defining props as a class or interface type is best and how to provide default values to optional props.
🌐
Atomizedobjects
atomizedobjects.com › blog › react › how-to-pass-a-react-component-as-a-prop-in-typescript
How to pass a React component as a prop in TypeScript | Atomized Objects
January 28, 2022 - To do this, you just need to add an open and closing carats (more than and less than symbols <>) and then pass in the types of the functional component, in our case these are saved in the interface ExampleFCProps so the final outcome for the type for that functional component in TypeScript ...
🌐
Carl Rippon
carlrippon.com › different-ways-to-strongly-type-function-component-props-with-typescript
Different ways to strongly-type function component props with TypeScript
April 7, 2020 - In the above example, we have extended the standard props type for an input element. This means our component now accepts props such as maxLength and placeholder. Alternatively we can use a type alias and intersection to achieve the same effect: ... A comprehensive guide to building modern React applications with TypeScript.
🌐
Dmitri Pavlutin
dmitripavlutin.com › typescript-react-components
How to Use TypeScript with React Components - Dmitri Pavlutin
September 29, 2021 - How to use TypeScript to type React components: validate props, mark props as optional.
🌐
LogRocket
blog.logrocket.com › home › how to type react children correctly in typescript
How to type React children correctly in TypeScript - LogRocket Blog
December 19, 2025 - PropsWithChildren takes your existing component prop and returns a union type with the children prop appropriately typed. No extra work from you is needed. Here’s the actual definition of the PropsWithChildren type: type PropsWithChildren<P> = P & { children?: ReactNode }; Essentially, it merges your props with an optional children field typed as ReactNode.
Top answer
1 of 7
101

The correct type for a functional component is React.FunctionComponent or React.FC which is a shortcut alias for it

import React, { FC } from 'react';

const getTabContent: FC = () => {
  switch (tab) {
    case 1:
      return <Images images={images} onSelect={onSelect}/>;
    default:
      return <Search onSelect={onSelect}/>;
  }
};

The FC type simply add the children property to the props argument of the functional component so you can access it:

const SomeComponent: FC = ({ children }) => (
  <div className="hello">{children}</div>
);

FC is a generic type so you can "add" props to your component:

interface SomeComponentProps {
  foo: string;
}

const SomeComponent: FC<SomeComponentProps> = ({ children, foo }) => (
  <div className={`Hello ${foo}`}>{children}</div>
);

Edit: React 18 update

Since React 18, FC doesn't add the children prop implicitly and offers an explicit way to do so with the PropsWithChildren generix type

Example:

type SomeComponentProps = { a: string };

const SomeComponent: FC<SomeComponentProps> = ({ a }) => <div>{a}</div>;

// This will fail when using the following expression
<SomeComponent>Hey I'm a child</SomeComponent>

Usage with children:

type ComponentWithChildrenProps = PropsWithChildren<{ a: string }>;

const ComponentWithChildrenProps: FC<ComponentWithChildrenProps> = ({
  a,
  children
}) => <div>{a} and {children}</div>

This allows to have a children prop a bit stricter. e.g.

type StrictCompProps = { children: string };

const StrictComp: FC<StrictCompProps> = ({ children }) => <div>{children}</div>;

// This will fail
<StrictComp><span>hey</span></StrictComp>
2 of 7
54

If you want to use FunctionComponent with class Component, Then use React.ComponentType

🌐
Fettblog
fettblog.eu › typescript-react › components
TypeScript and React: Components
Creating a type for our properties, and telling TypeScript that the parameters of our functional component are of that type. You already get nice suggestions in VS Code: And errors when you compile without passing all required properties: If you want to make some properties optional, do that in the respective Props type: type CardProps = { title: string, paragraph?: string // the paragraph is optional } There’s a generic type you can use. import React, { FunctionComponent } from 'react'; // importing FunctionComponent type CardProps = { title: string, paragraph: string } export const Card: FunctionComponent<CardProps> = ({ title, paragraph }) => <aside> <h2>{ title }</h2> <p> { paragraph } </p> </aside> const el = <Card title="Welcome!" paragraph="To this example" />
🌐
TypeScript
typescriptlang.org › docs › handbook › jsx.html
TypeScript: Documentation - JSX
As the name suggests, the component is defined as a JavaScript function where its first argument is a props object. TS enforces that its return type must be assignable to JSX.Element. ... Because a Function Component is simply a JavaScript function, function overloads may be used here as well: ...
Top answer
1 of 2
42

You want to pass a component constructor, not a component instance:

import * as React from 'react';
import { Route, RouteProps } from 'react-router';

interface Props extends RouteProps {
    component: React.ComponentType;
}

const PrivateRoute = ({ component: Component, ...rest }: Props) => {
    return (
        <Route
            {...rest}
            render={(props) => <Component {...props} />}
        />
    );
};

export default PrivateRoute;

class Foo extends React.Component {

}
let r = <PrivateRoute component={Foo} path="/foo" />

Edit

A more complete solution should be generic and use RouteProps instead RouterProps:

import * as React from 'react';
import { Route, RouteProps } from 'react-router';

type Props<P> =  RouteProps & P & {
    component: React.ComponentType<P>;
}

const PrivateRoute = function <P>(p: Props<P>) {
    // We can't use destructuring syntax, because : "Rest types may only be created from object types", so we do it manually.
    let rest = omit(p, "component");
    let Component = p.component;
    return (
        <Route
            {...rest}
            render={(props: P) => <p.component {...props} />}
        />
    );
};

// Helpers
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];  
type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>; 
function omit<T, TKey extends keyof T>(value:T, ... toRemove: TKey[]): Omit<T, TKey>{
    var result = Object.assign({}, value);
    for(let key of toRemove){
        delete result[key];
    }
    return result;
}


export default PrivateRoute;

class Foo extends React.Component<{ prop: number }>{

}
let r = <PrivateRoute component={Foo} path="/foo" prop={10} />
2 of 2
6

After a few hours and some investigation, here is the solution that fits my requirements:

import * as React from 'react';
import { Route, RouteComponentProps, RouteProps } from 'react-router';

const PrivateRoute: React.SFC<RouteProps> =
  ({ component: Component, ...rest }) => {
    if (!Component) {
      return null;
    }
    return (
      <Route
        {...rest}
        render={(props: RouteComponentProps<{}>) => <Component {...props} />}
      />
    );
  };

export default PrivateRoute;

  • No any;
  • No extra complexity;
  • Composition pattern retained;
🌐
Reddit
reddit.com › r/reactjs › react typescript. how can i get props type from a component?
r/reactjs on Reddit: React typescript. How can I get props type from a component?
October 8, 2021 -

Hi,all.

I want to defined a Link component that has an as props. For example, using react router link component. <Link as={ReactRouterLink} to="/">Page</> . In this case, even I know the types of react router link, for example to props, but I don't want to define it on the Link. Is there a way to define the types of Link that can extend the type from as component?