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)

Most upvoted comments

still relevant! had to change the import from import type { TFunction } from 'react-i18next' to import type { TFunction } from 'i18next'

This works for me:

const t: TFunction = (arg: Array<string> | string): TFuncReturn<string, string, any> => {
    return typeof arg === 'string' ? arg : arg[0];
};

That’s a function overload limitation because TFunction can have multiple shapes, but it can’t determine the right one if it’s not used on the actual t function. There are a few things that I can do to remove “weight” from the TFunction and will fix your problem, I’ll keep you posted.

Can you try with 22.0.3?