react-i18next: Unable to initialize TFunction for testing after latest version
🐛 Bug Report
Hi. I’ve initialized TFunction in my tests before i18next v22 and i18next v12, but I’m not able to do that after the upgrade.
To Reproduce
Code before the upgrade
import { TFunction } from 'react-i18next';
import { translationResources } from '../../constants';
const translation: { [key: string]: string } = translationResources.fi.translation;
const t: TFunction<'translation', undefined> = (str: string) => translation[str];
Code after the upgrade
import { TFunction } from 'i18next';
import { translationResources } from '../../constants';
const translation: { [key: string]: string } = translationResources.fi.translation;
const t: TFunction<'translation', undefined> = (str: string) => translation[str];
The error
Type '(str: string) => string' is not assignable to type 'TFunction<"translation", undefined>'.
Type 'string' is not assignable to type 'TFunctionDetailedResult<any>'.
Expected behavior
I expect to be able to initialize TFunction
Your Environment
- runtime version: node v16
- i18next version: 22.0.2
- os: macOS
- typescript: 4.7.4
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 21 (9 by maintainers)
still relevant! had to change the import from
import type { TFunction } from 'react-i18next'toimport type { TFunction } from 'i18next'This works for me:
That’s a function overload limitation because
TFunctioncan have multiple shapes, but it can’t determine the right one if it’s not used on the actualtfunction. There are a few things that I can do to remove “weight” from theTFunctionand will fix your problem, I’ll keep you posted.Can you try with 22.0.3?