Skip to content
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

Type annotation needed only when component is root of exported component? #8166

Closed
FezVrasta opened this issue Oct 29, 2019 · 3 comments
Closed

Comments

@FezVrasta
Copy link
Contributor

FezVrasta commented Oct 29, 2019

Flow version: 0.110.1

Expected behavior

The un-annotated component should not need any type annotations.

Actual behavior

Flow requires me to annotate a component if I'm gonna use it inside an exported component, but this happens only if the un-annotated component is rendered as root of the exported component.

If I wrap the un-annotated component with something else (a fragment, a div, etc), then Flow doesn't require me to annotate it.

I know Flow wants us to annotate the exported functions, components, etc... but I'm not exporting that component, I'm using it inside a component that's getting exported.

Why is it behaving this way?

@nmote
Copy link
Contributor

nmote commented Oct 30, 2019

This is because Flow infers the return type of Ko to be something like React$Element<typeof ContainerB>, which causes the type of ContainerB to end up in the externally-visible interface. This means that all input positions must be annotated. If you don't want that type to end up in the externally-visible interface, you can use a more generic return type annotation for Ko, like React$Node. See this playground example

@FezVrasta
Copy link
Contributor Author

Thank you for the information!

What's the need to use React$Element<typeof ContainerB> as return type? Shouldn't be enough to return one of the types that compose React$Node (for example, if my component returns an array?

@nmote
Copy link
Contributor

nmote commented Oct 30, 2019

In most cases React$Node should work just fine. Flow just infers the more specific type by default.

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

No branches or pull requests

2 participants