-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
TypeScript support #348
TypeScript support #348
Conversation
Thank you for working on this. This is amazing.
This is a tricky problem. I'm not sure about a clear answer. If we would add the docs for both type systems to one key we need to make sure they are always compatible and always stay compatible. Additional features which do not exist in the other language are okay I guess, but features that just would need a slightly different documentation could make things complicated. Maybe for example differences in enum types or mapped types? I can't think of any concrete example but I wonder if it would make it complicated for tools using react-docgen which support components written in flow and ts.
I think we should rename it, but we don't have to do it in this PR. I would also classify this a breaking change, because I saw some custom resolvers using internal utils of react-docgen. Also in addition what I said to 1. what do you think about adding a key to the documentation that indicates which language we were parsing, js, flow or typescript? But also not sure if this would be useful to users. Maybe @okonet or @sapegin have input about what would make it easy to use in react-styleguidist? |
Thanks for your thoughts on this. I think we should have a separate |
I think it also makes the most sense to keep |
I think this is ready for review! I adjusted the output to use Also added support for generics to both Flow and TypeScript. It will inline type parameters when merging extended interfaces or other type references. This should help allow you to reuse generic types across components while keeping the docs understandable. Only waiting on recast to update their dep on ast-types: benjamn/recast#590 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much. It took some time to look through everything. I added some inline comments about some small changes and one question about the documentation of function signatures, but besides that it looks nice and clean to me.
src/utils/getTypeParameters.js
Outdated
declaration: NodePath, | ||
instantiation: NodePath, | ||
inputParams: ?TypeParameters, | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a return type here? Which I guess is TypeParameters
?
src/utils/getTypeParameters.js
Outdated
? paramPath.get('default') | ||
: null; | ||
const typePath = | ||
i < instantiation.node.params.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
micro optimizing: The whole length lookup could be done before the loop.
@@ -43,7 +43,7 @@ | |||
"commander": "^2.19.0", | |||
"doctrine": "^3.0.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update @babel/core
to ^7.4.4
so we ensure that typescript is fully supported.
Also noticed we have it in dependencies and dev dependencies, so I guess we can remove it from dev.
Thanks for the review @danez! Updated based on your comments. |
So I upgraded babel-core, and am now getting a bunch of errors about undefined exports in the tests... :| |
I will have a look |
Closes #325.
Depends on benjamn/ast-types#330
This adds TypeScript support to react-docgen. It automatically replaces the flow plugin with the typescript plugin in babel-parser when a
.ts
or.tsx
file is seen.The output follows the existing flow output format since TypeScript types are quite similar to Flow. The implementation follows the existing flow implementation quite closely as well - a new
getTSType
file is added similar to thegetFlowType
one, and the surrounding code is updated to also handle TypeScript annotations in addition to Flow.This brings up several questions about API and code organization.
flowType
to something more generic, add an additionaltsType
key, or just leave it as is?A few things left to do on this: