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

allow iterable with objects as children #1531

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ts4.1/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ declare module 'i18next' {
type ObjectOrNever = TypeOptions['allowObjectInHTMLChildren'] extends true
? Record<string, unknown>
Copy link

Choose a reason for hiding this comment

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

I think this would be cleaner:

type HTMLChild = ReactNode | ObjectOrNever;
declare module 'react' {
  interface HTMLAttributes<T> {
    children?: HTMLChild | Iterable<HTMLChild>;
  }
}

I've used Iterable because that's what react itself uses for ReactNode's circular reference, and given the type a specific name so that ObjectOrNever still actually represents ObjectOrNever.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, updated MR. Thx for the suggestion!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interestingly enough Iterable does not work correctly when combined with t function, but Array works as expected 🤔
Here's a code example:

<div>
    {t('hello')}
    <span>Hi</span>
</div>

I get a compilation error on outer div

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Either disabling allowObjectInHTML or changing Iterable to Array fixes it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error is:

This JSX tag's 'children' prop expects a single child of type 'ReactI18NextChild | Iterable<ReactI18NextChild>', but multiple children were provided.ts(2746)

: never;

type ReactI18NextChild = React.ReactNode | ObjectOrNever;

declare module 'react' {
interface HTMLAttributes<T> {
children?: ReactNode | ObjectOrNever;
children?: ReactI18NextChild | Iterable<ReactI18NextChild>;
}
}

Expand Down