-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Using React.FC does not properly extract prop types #1989
Comments
Maybe it's not the solution you are looking for as I agree it should work. We are refactoring our code base in our UI library to stop using |
The article is a bit outdated.
In React, a function component is not just a function which returns JSX children. It also has some static properties such as displayName and propTypes (though prop types are discouraged in favour of parameter defaults). If you don't type the function, you lose typings on the static properties.
Perhaps when the article is written, however React exposes
I suppose whether this is a pro/con is up to the maintainers of this library. At my work we're only interested in React support.
defaultProps can be used, however React is recommending using function parameters. See tweet by Dan Abramov and RFC. |
In our case, we don't need static props. Regarding preact, we don't use it either. What I see after migrating to my approach is:
|
In my styleguide.config.js: const reactDocgenTypescript = require('react-docgen-typescript');
// You need this or else you will get a ton of props if your component also includes something like React.ComponentPropsWithoutRef<'div'>
const options = {
propFilter: (prop) => {
if (prop.parent) {
return !prop.parent.fileName.includes('node_modules');
}
return true;
},
};
module.exports = {
// other config...
propsParser: reactDocgenTypescript.withCustomConfig(`${process.cwd()}/tsconfig.json`, options).parse,
}; Defaults do not work unless you have And yeah, I'm also considering dropping |
Current behavior
We're migrating parts of our app to typescript slowly, and leaning pretty heavily on
React.FC
to correctly set prop and return types on our component. When you cast a component using only React.FC, styleguidist does not display the Props and Methods field.I tested this on both 11.2.0 and on 12.0.0-alpha9.9 and the issue is present on both.
To reproduce
This component will fail to produce a
Props & Methods
fieldThis component will produce a
Props & Methods
field, but thetype
arg for each prop will be empty. Onlydefault
field will have values.Weirdly, casting the props directly will produce the correct results regardless of how
defaultProps
are provided. So there's likely some logic to extract the correct types from the args that are not being correctly applied to React.FC.Expected behavior
Usage of React.FC extracts the correct prop types to display in the "Props & Methods" field.
The text was updated successfully, but these errors were encountered: