reactjs/react-docgen

Better TS enum support

Opened this issue · 2 comments

Reporting from storybookjs/storybook#24165

Currently TS enums analysis is lossy:

enum ButtonType {
  Button = 'button',
  Reset = 'reset',
  Submit = 'submit',
}

type ButtonTestProps = {
  type?: ButtonType;
};

export const ButtonTest = (props: ButtonTestProps) => {
  return <button {...props}>Test</button>;
};

Produces:

          "tsType": {
            "name": "ButtonType"
          },

Compare this to a union:

type ButtonType = "button" | "reset" | "submit";

Which produces:

          "tsType": {
            "name": "union",
            "raw": "\"button\" | \"reset\" | \"submit\"",
            "elements": [
              {
                "name": "literal",
                "value": "\"button\""
              },
              {
                "name": "literal",
                "value": "\"reset\""
              },
              {
                "name": "literal",
                "value": "\"submit\""
              }
            ]
          },

It would be fantastic if react-docgen could produce something similar for enums!

I believe this is not the case anymore...? When i test enums in our project, i got this output:

"variant": {
  "tsType": {
    "name": "union",
    "raw": "\"standard\" | \"primary\"",
    "elements": [
      {
        "name": "literal",
        "value": "\"standard\""
      },
      {
        "name": "literal",
        "value": "\"primary\""
      }
    ]
  },
  "required": false,
  "description": "The variant of the button.",
  "defaultValue": {
    "value": "\"standard\"",
    "computed": false
  }
}

the typescript type looks like this:

export type ButtonProps = {
  /** The variant of the button. */
  variant?: "standard" | "primary";
};

@shilman maybe this is actually a storybook integration problem instead. It seems that the enum is available.
cc @valentinpalkovic