Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Add "import React from 'react'" statement to tsx files #342

Closed
hamza0867 opened this issue Dec 23, 2019 · 7 comments · Fixed by #343
Closed

Add "import React from 'react'" statement to tsx files #342

hamza0867 opened this issue Dec 23, 2019 · 7 comments · Fixed by #343

Comments

@hamza0867
Copy link

Hi,

I would love if the generated .tsx files would import React so that other libraries could take advantage of the exported types from "@types/react".

My specific use case is the integration of the tsx files with the documentation site generator docz, specifically the Props component. Where all it needs is the component definition to generate the documentation for the props of a react component. But it needs React to be imported in the tsx file ( which makes sense to me )

Here is what the docs look like when there is no import React from "react" statement in the generated .tsx files.

Screenshot from 2019-12-23 13-30-14

Here is what the docs look like when there is an import statement of React in the generated .tsx files:

Screenshot from 2019-12-23 13-33-11

I think the relevant line of code to change is this one

Thank you so much

@cristianoc
Copy link
Collaborator

@hamza0867 I would rather not emit an import for react on all the .tsx files.
As a first step: #343
It only emits the import if the file exports one function component.
What's the full extent of your use case?

@hamza0867
Copy link
Author

@cristianoc I believe that's all I need to make it work. I just need the .tsx files who use React type annotations to import react. There is another small tweak that I would love to have is to annotate react components with React.FunctionComponent instead of React.ComponentType but that is something that I will have to do on my own ( since it doesn't make sense to have all components be function components especially for backwards compatibility ).

@hamza0867
Copy link
Author

Ok so I just changed the module resolution in tsconfig so I don't need the change from React.ComponentType to React.FunctionType.
All I need is for genType to import React in files where React is used.

Thank you so much

cristianoc added a commit that referenced this issue Dec 24, 2019
* In TS, emit import react when a react component is exported.

Fixes #342

* Docs.
@cristianoc
Copy link
Collaborator

@hamza0867 creating release 3.9.0 now.

@hamza0867
Copy link
Author

Thank you so much

@cristianoc
Copy link
Collaborator

@hamza0867 if this is for your attempt to publish TS libraries, would you write a few words on the outstanding blocks you have found?
I can only guess doc generation expects TS code to be in a specific format, though it would be interesting to confirm what that is.

@hamza0867
Copy link
Author

@cristianoc Sorry for the late response. I will be setting a repository where I put everything that I found and the main blockers that I encountered ( and still encountering )

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants