Fez Vrasta
Fez Vrasta

Reputation: 14815

Extract props type from React component

I'm trying to understand how (and if it's possible) to extract the props type from a given component.

const Component = (props: { color: string }) => <div {...props} />;

type ComponentProps = $ExtractArguments<typeof Component>[0] // pseudo-code

I found this utility googling, but I don't understand if it can be useful...

type $Arguments<F> = $Call<<A: $ReadOnlyArray<mixed>>((...A) => mixed) => A, F>;

Upvotes: 0

Views: 246

Answers (1)

Alex Savin
Alex Savin

Reputation: 528

You're looking for React.ElementProps or React.ElementConfig. The last takes into account defaultProps, so it's more useful in practice.

Docs

Try

import * as React from 'react';

const Component = (props: { color: string }) => <div {...props} />;

type Props = React.ElementConfig<typeof Component>;

Upvotes: 1

Related Questions