Extract the type of a component’s props
8 May 2025 (Updated 18 May 2025)
Suppose you have this component:
import React from 'react';
interface Props {
name: string;
age?: number;
}
const SomeComponent: React.FC<Props> = ({ name, age }) => (
<div>{name} - {age}</div>
)
You can extract the type of <SomeComponent>
like so:
type PropsOfSomeComponent = React.ComponentProps<typeof SomeComponent>
const test: PropsOfSomeComponent = { name: 'Alice' } // ✅ age is optional
This can be useful if you have some third-party component from an NPM package but that package doesn’t export the component’s types.
Tagged:
React recipes