react-docgen icon indicating copy to clipboard operation
react-docgen copied to clipboard

Confused react type names on Flow and Typescript

Open xiaoshuangLi opened this issue 6 years ago • 2 comments

Hi, I try convert react-docgen props to JSON-schema. But in typescript and flow, we get different names by different ways to program.Just like this. This ReactReactNode makes me so confused. How should i do with it ?

import React, { ReactNode, Component } from 'react';

interface Props {
  content: ReactNode,
  children: React.ReactNode,
}

class TSReactComponent extends Component<Props> {
  render() {
    return (
      <div>hello world</div>
    );
  }
}

export default TSReactComponent;

react-docgen result

{
  "description": "",
  "displayName": "TSReactComponent",
  "methods": [],
  "props": {
    "content": {
      "required": true,
      "flowType": {
        "name": "ReactNode"
      },
      "description": ""
    },
    "children": {
      "required": true,
      "flowType": {
        "name": "ReactReactNode",
        "raw": "React.ReactNode"
      },
      "description": ""
    }
  }
}

xiaoshuangLi avatar Oct 24 '19 07:10 xiaoshuangLi

I'm also having issues where the parser is thinking I'm using Flow when I'm using typescript. As a result, I get parsing errors because not all TypeScript is valid Flow syntax.

greypants avatar Jan 23 '20 16:01 greypants

I worked around this by looking at the react-docgen demo code, and noticing that they were passing parserOptions.plugins, and manually specifying the language there:

This ended up working for me:

parse(sourceString, undefined, undefined, {
  filename,
  parserOptions: { plugins: ["jsx", "typescript"] }
})

But you would think it would just infer it from the filename...

greypants avatar Jan 28 '20 20:01 greypants