react-i18next: Typescript error when using variables in Trans component
🐛 Bug Report
Typescript throws when using variable in Trans component. Maybe a bit of an edge case, but when using a custom component that expects string as children within Trans
and the child to this component should include a variable Typescript throws an error. Also it is impossible to ignore TS in this situation.
To Reproduce
Note that this is a minimal reproduction (e.g. i18next is not fully set up).
One of the conditions for this bug is that you have the child in a separate line. If they would be in one line, you could add a `{/* @ts-ignore */} before hand. However it is not working when the code is in multiple lines.
Expected behavior
TS doesn’t throw or at least there is a way to ignore it.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 22 (5 by maintainers)
I’m experiencing something similar:
yields:
TS2322: Type '{ title: string; }' is not assignable to type 'ReactNode'. Object literal may only specify known properties, and 'title' does not exist in type 'ReactElement<any, string | JSXElementConstructor<any>> | ReactFragment | ReactPortal'.
I found an solution for this, you can pass
values
toTrans
component like this:@BrooceLR I tested this,
allowObjectInHTMLChildren
works like disabling the TypeScript warning globally. It suppresses warnings in all JSX. So this is not a good solution.Wouldn’t that suppress typescript errors on any children prop? Objects are not valid JSX children, meaning that outside
Trans
components I’d expect it to crash the runtime?Everyone who is still facing this problem, please set
allowObjectInHTMLChildren
totrue
in yourreact-i18next
type declaration file. You can follow the instructions here to learn how to create a type declaration file, it should look similar to this one:If the error still persists, please share a minimalistic version of your app (using the Trans component), and I’ll help you.
I’m not a TypeScript user, but if it works like this:
it may be your StringWrapper component is doing something that is causing the problem.