-
-
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
withTranslation - Type error: Type 'TFunctionResult' is not assignable to type 'ReactNode' #1559
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I'm getting the same error in the v12 release for the |
Similar error here with the v12 release,
|
fyi: i18next/i18next#1852 |
See https://www.i18next.com/overview/typescript for how to add the required type definitions. This will make the error go away. But more importantly, i18next will now ensure you don't pass arbitrary strings to the By the way, thanks to everyone who made this happen! This is awesome! Finally we can statically ensure correct usage of the |
And what if you need to pass "arbitrary strings" or at least strings formed by concatenation? Something like: function component(props: {stringProp: string}) {
const {t} = useTranslation(myNs);
return <div>{t(props.stringProp)}</div>
} To strictly type all strings with templates is not always an option. Thank you! |
@SergioTx I think it makes sense to refactor such that the props are already translated. If this is not an option, you could use |
Is there any way to disable type-checking here? In our translation workflow, we define the keys in code first with a default value... and then the translation keys are extracted by and automated process. So we have code-first approach when it comes to the keys, while the typescript docs effectively describe a translation-first approach, where the translation keys already exists in a JSON file. |
|
The same error with the
|
can you try with i18next v22.0.3? |
It works now. No compilation errors. |
@MarcelGeo can we close this? |
I have the same problem with 22.0.4: |
Can you create a reproducible example an open a new issue? |
ok, screw sandbox, I made a demo on top of playground project: #1574 |
Hey @kirill-linnik, it seems |
I'm seeing something similar. As as temporary workaround I've been able to "fix" this by overriding the
In file `i18next.d.ts`:import 'i18next';
declare module 'i18next' {
interface TFunction<
N extends Namespace = DefaultNamespace,
TKPrefix = undefined
> {
<
TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A
? A
: never,
TDefaultResult extends string = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[]
): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
<
TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A
? A
: never,
TDefaultResult extends stringWithObject = object,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> & {
returnDetails: true;
returnObjects: true;
}
): TFunctionDetailedResult<TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>>;
<
TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A
? A
: never,
TDefaultResult extends string = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> & { returnDetails: true }
): TFunctionDetailedResult<TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>>;
<
TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A
? A
: never,
TDefaultResult extends stringWithObject = object,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> & { returnObjects: true }
): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
<
TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A
? A
: never,
TDefaultResult extends string = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap>
): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
<
TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A
? A
: never,
TDefaultResult extends string = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
defaultValue?: string,
options?: TOptions<TInterpolationMap> | string
): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
}
}
|
@badsyntax yeah, there are several ways to workaround it. I've decided to postpone the update until it is fixed properly @pedrodurek seems like we need |
If you have access to the |
@pedrodurek we have also ran into this same issue while upgrading a few of our projects. For now I'm working around this with the following (based on the comment here):
import 'react-i18next';
declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: 'common';
resources: {
[key: string]: {
[key: string]: string;
};
};
}
} We found that doing what the docs suggested and importing all of our namespace/JSON files resulted in a painfully slow VSCode. Further more, our projects use For what its worth I was able to import these into the import common from '@/../public/static/locales/en/common.json';
import home from '@/../public/static/locales/en/home.json';
...
declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: 'common';
resources: {
common: typeof common;
home: typeof home;
...
};
}
} While the IDE prompts were nice (aside from the slowdown), I did run into a few other problems:
// common.json
{
"errors": {
"unexpected": "Unexpected error"
}
}
// home.json
{
"page": {
"title": "Welcome"
}
} // Relying on DefaultNS (common)
const { t } = useTranslation('home', { useSuspense: true });
console.log(✅, t('page.title'))
console.log(❌, t('common:errors.unexpected')) // Explicitly including "common"
const { t } = useTranslation([ 'common', 'home' ], { useSuspense: true });
console.log(✅, t('home:page.title'))
console.log(✅, t('common:errors.unexpected'))
console.log(✅, t('common:dates.days.mon'))
console.log(❌, t(`common:dates.days.${day}`)) With all of this in mind, and realizing my usecases may be the minority, it would still be great to see a way to bypass/disable the typing requirements as an option. For now the workaround mentioned above will work for me. Hope its helpful to others. |
Hey @is-jonreeves. // Relying on DefaultNS (common)
const { t } = useTranslation('home', { useSuspense: true });
console.log(✅, t('page.title'))
console.log(❌, t('common:errors.unexpected')) This pattern ☝️ is not supported due to compilation time constrains, I'd advice to explicitly include the "common" namespace. I'm working on some improvements that will allow us to do that, but it may require a lot of tests and benchmark. console.log(✅, t('common:dates.days.mon'))
console.log(❌, t(`common:dates.days.${day}`))
const day = 'today' as const;
// type inferred: 'today' instead of string
console.log(✅, t(`common:dates.days.${day}`)) with union type: type Props = {
day: 'today' | 'tomorrow' | 'yesterday'
}
const Component = ({day}: Props) => {
console.log(✅, t(`common:dates.days.${day}`))
} |
should be fixed with i18next v22.0.5 |
v22.0.5 didn't fix it for me unfortunately. I am still getting the following error and had to downgrade to 22.0.3:
I'm using the following with Node 18.12.1:
And if it helps, this is the project: https://github.com/eiskalteschatten/InOrdnung |
Have the exact same problem. |
// i18next.d.ts
import 'i18next';
declare module 'i18next' {
interface CustomTypeOptions {
returnNull: false;
...
}
} I also recommend updating your i18next configuration to behave accordantly. https://www.i18next.com/overview/configuration-options#translation-defaults i18next.init({
returnNull: false,
...
}); |
Does not seem to work for me...
|
@schankam make sure you define |
in my case changing |
Until the problem not fixed, we can use some sort of crutch like: function useXTranslation() {
const {t} = useTranslation()
return {t: t<string>};
} and replace my versions of i18next
NOTE: possibly this is insane advice. I am playing with TS/JS for only 5 days. But this is working for my case. 🤣 |
Have you tried with i18next v22.4.5 ? |
@adrai I think yes, because my version is $ npm ll | grep i18next
├── [email protected]
├── [email protected]
├── [email protected] This is looks like I use exactly |
a minimal reproducible example repository would help |
Almost all people in this thread is describe the same problem with type definitions. No new info in my case. If you need an additional info - feel free to ask. |
I don't see any |
wow. this is working. thx. But also, I think that this functional should be working without additional manipulations around the code. Hope we will get a function that will return just a strings. |
Like described, the t function is potentially returning null... but it might be in the next major version we will set the default for |
any expected date on when it could be released ? otherwise we can keep using the old version |
@m-nathani no ETA at the moment... waiting for @pedrodurek's feedback... |
Sounds great.. thanks for the link 👍 |
Type error: Type 'TFunctionResult' is not assignable to type 'ReactNode'
This typescript error is not fixed by using withTranslation HOC.
The text was updated successfully, but these errors were encountered: