Created
September 5, 2018 22:29
-
-
Save disintegrator/05bc0687b5c839869c3336598db0610e to your computer and use it in GitHub Desktop.
Get the prop types of a react component class, functional component or string corresponding to HTML tag name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
/** | |
* Given a JSX.IntrinsicElement[...], get its attributes | |
*/ | |
type Attributes<E> = E extends React.DetailedHTMLProps<infer Attr, any> | |
? Attr | |
: never; | |
/** | |
* Given a react component class, functional component or string that may be an | |
* HTML tag return its prop types | |
*/ | |
type PropTypes<C> = C extends React.ComponentType<infer Props> | |
? Props | |
: C extends keyof JSX.IntrinsicElements | |
? Attributes<JSX.IntrinsicElements[C]> | |
: never; | |
class Point extends React.Component<{ x: number; y: number }> {} | |
type PointProps = PropTypes<typeof Point>; | |
// { x: number; y: number } | |
const Animal: React.SFC<{ species: string }> = props => ( | |
<div>{props.species}</div> | |
); | |
type AnimalProps = PropTypes<typeof Animal> | |
// { species: string } | |
type InputProps = PropTypes<'input'> | |
// React.InputHTMLAttributes<HTMLInputElement> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment