reactjs / react-docgen

A CLI and library to extract information from React component files for documentation generation purposes.

Home Page:https://react-docgen.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implicitly typed `props` in FunctionComponent not found

brandongregoryscott opened this issue · comments

Given the following component structure:

import React from "react";

interface ButtonProps {
  onClick: () => void;
}

const Button: React.FC<ButtonProps> = (props) => {
  return <button {...props}>Hello world</button>;
};

export default Button;

react-docgen returns an object without any props:

{
  "description": "",
  "displayName": "Button",
  "methods": []
}

However, if props is explicitly typed, the expected props are returned:

const Button: React.FC<ButtonProps> = (props: ButtonProps) => {
{
  "description": "",
  "displayName": "Button",
  "methods": [],
  "props": {
    "onClick": {
      "required": true,
      "tsType": {
        "name": "signature",
        "type": "function",
        "raw": "() => void",
        "signature": {
          "arguments": [],
          "return": {
            "name": "void"
          }
        }
      },
      "description": ""
    }
  }
}

Given that TypeScript properly detects props as ButtonProps without the explicit typing (pulled from React.FC<ButtonProps> type of the component itself), it feels like this shouldn't be a required case to parse props correctly. Is there any easy way to tweak the existing resolver to read the generic type of React.FC?

Thanks for reporting.
You are right that this totally should work.
We will have a look soon.

Can you describe how to attack this problem? I'd like to try working on a PR.

Fixed in 7.0