-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
97f92a0
to
c5b3c2b
Compare
@@ -100,7 +100,7 @@ declare module 'i18next' { | |||
} | |||
|
|||
type ObjectOrNever = TypeOptions['allowObjectInHTMLChildren'] extends true | |||
? Record<string, unknown> |
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.
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
.
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.
True, updated MR. Thx for the suggestion!
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.
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
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.
Either disabling allowObjectInHTML
or changing Iterable
to Array
fixes it
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.
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)
typescript will now allow core react HTML tags to accept multiple nodes mixed with translation objects as children
c5b3c2b
to
4e045a0
Compare
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.
Looks great to me! Thanks!
included in v11.18.1 |
This PR fixes TypeScript error which prevents multiple nodes to be used inside core React elements when
allowObjectInHTMLChildren
is set to true.Code example that fails on the latest published version with React 18 but works correctly with this PR:
Resolves #1506
Checklist
npm run test
Checklist (for documentation change)