Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract proptypes in Typescript (interfaces) #77

Closed
SimpleCookie opened this issue Jan 18, 2019 · 3 comments
Closed

Extract proptypes in Typescript (interfaces) #77

SimpleCookie opened this issue Jan 18, 2019 · 3 comments

Comments

@SimpleCookie
Copy link

How can I extract the proptypes (the interface) of a component in Typescript? I Know I can access the component and get the props easily, but I want to get the types, so that I can easily generate a nice show-case for my lib components.

@ferdaber
Copy link
Collaborator

Are you asking for the .propTypes static property of a component, or just getting the type of the props for a component?

To get the type of what a component expects to be passed in (i.e. "external" or "apparent" props), you can use:

import SomeComponent from 'external-library'

type InternalProps = React.ComponentProps<typeof SomeComponent>
type ApparentProps = JSX.LibraryManagedAttributes<typeof SomeComponent, InternalProps>

@SimpleCookie
Copy link
Author

Are you asking for the .propTypes static property of a component, or just getting the type of the props for a component?

To get the type of what a component expects to be passed in (i.e. "external" or "apparent" props), you can use:

import SomeComponent from 'external-library'

type InternalProps = React.ComponentProps<typeof SomeComponent>
type ApparentProps = JSX.LibraryManagedAttributes<typeof SomeComponent, InternalProps>

I was able to retrieve the props passed down to a component, but I wanted to get the whole interface, even the props which aren't passed down. But I'll look into the things @sw-yx mentioned this week and see if it can solve my problem. I'll also look into the code example you gave me, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants