amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.63k stars 236 forks source link

Type of TranslationValue for `t.rich` should include Element.JSX #1567

Open votrungquan1999 opened 4 days ago

votrungquan1999 commented 4 days ago

Description

When using t.rich for rich text format, I see that I can use JSX.Element for the value, which displays correctly. However, the typescript gave me the error.

Type 'Element' is not assignable to type 'TranslationValue | ((chunks: ReactNode) => ReactNode)'

I think the library used under the hood also supports JSX.Element when formatting rich text. we could improve the typing here.

Another issue is that in the server component that renders the formatted key, this error happens

[ Server ] Each child in a list should have a unique "key" prop.

Verifications

Mandatory reproduction URL

https://github.com/votrungquan1999/reproduce-next-intl-typing

Reproduction description

  1. Run npm run dev
  2. see that the Seconds component is rendered by the t.rich
  3. See the error report on the left side

https://github.com/user-attachments/assets/fe348f0e-a894-42d6-a08c-194f0769055b

Expected behaviour

amannn commented 4 days ago

There's a note on this in the docs: How can I use “self-closing” tags without any chunks?.

This currently has some benefits related to upcoming type-safety of ICU args in the upcoming next-intl@4.0. If we'd accept ReactNode, this would for example also allow number, therefore defeating number formatting validation.

Due to this, I think it's not worth it changing this behavior for the time being.