-
Notifications
You must be signed in to change notification settings - Fork 258
Description
Hi there,
As my components get increasingly complex, I've been struggling to get react-docgen-typescript to parse them. In particular, I have
- props interfaces extending other interfaces
- components using ANDed (&) intersection props interfaces
- higher-order components that change returned prop types
Right now, react-docgen-typescript can't cope with any of those because it's trying to look up interfaces by name by hand, and do other jiggery-pokery which can't work in the general case.
I think the only general solution to making this work properly is to engage the help of the TypeScript compiler itself to process the file and expose the type information. (It appears to be relatively little code to get the compiler to do that.)
I'd be happy to help out with modifying react-docgen-typescript to use this approach, but wanted to check in with you before beginning on it as it's not an entirely trivial change.
Proof of Concept
I've developed a little (~120 lines) proof of concept standalone app which is capable of parsing any component file I've thrown at it yet. It simply asks TypeScript to give it the resolved type for any exported objects, and can scan that and extract props successfully.
Demo app (both TS and the compiled JS): https://gist.github.com/RoystonS/f590f336643a782ac5abb5059817e06c
That app takes a filename as a parameter and parses the file. If you have an especially complex setup, it'll also optionally take a tsconfig file to aid the TS compiler, but works in most cases without it.
Sample nasty test component (indicative of the types we're using with glamorous) and the output from the program:
https://gist.github.com/RoystonS/92784110dc263de4eea5329142badc5d
So...
Would you be happy for me to begin modification of react-docgen-typescript on a branch, to use this approach? Or would you rather do it? As far as I can make out, it would close a number of existing issues too.