Reputation: 14815
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
Reputation: 528
You're looking for React.ElementProps or React.ElementConfig. The last takes into account defaultProps
, so it's more useful in practice.
import * as React from 'react';
const Component = (props: { color: string }) => <div {...props} />;
type Props = React.ElementConfig<typeof Component>;
Upvotes: 1