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

Add ReactNode type docs #627

Merged
merged 3 commits into from
Jun 3, 2023
Merged

Add ReactNode type docs #627

merged 3 commits into from
Jun 3, 2023

Conversation

filiptammergard
Copy link
Collaborator

@filiptammergard filiptammergard commented May 31, 2023

No description provided.

}

function Component({ children }: Props) {
return <>{children}</>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return <>{children}</>;
return children;

Latest types and TS will support that and that's the proper default. Everything else are workarounds for bugs. We have those documented I think somewhere and should link to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely should be the default. The problem I get (with latest @types/react and typescript) is that <Component> implicitly gets ReactNode as return type, which gives type error when I try to render <Component> from another component:

import { ReactNode } from "react";

interface Props {
  children?: ReactNode;
}

function Component({ children }: Props) {
  return children;
}

function Example() {
  return <Component>Hello</Component>; // 'Component' cannot be used as a JSX component. Its return type 'ReactNode' is not a valid JSX element.
}

TypeScript playground

Should this not produce a type error in latest @types/react and typescript?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now the TypeScript playground I linked just magically works. Amazing! Do you think we need to inform what version are required for this or should we should assume latest (as we actually state we do)?

docs/react-types/ReactNode.md Outdated Show resolved Hide resolved
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

@filiptammergard filiptammergard merged commit dd8176d into main Jun 3, 2023
6 checks passed
@filiptammergard filiptammergard deleted the ReactNode branch June 3, 2023 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants