You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An instance of TFunction<...> given from useTranslation(['a', 'b']) cannot be passed as argument to TFunction<'a'>.
To Reproduce
constTYPES=['x','y']asconst;typeType=typeofTYPES[number];constgetTitleA: Record<Type,(t: TFunction<'a'>)=>string>={x: (t)=>t('a:x'),y: (t)=>t('a:y'),};constFoo: FC=()=>{const[tAstr]=useTranslation('a');const[tAarray]=useTranslation(['a']);const[tAB]=useTranslation(['a','b']);for(consttypeofTYPES){// okgetTitleA[type](tAstr);// TS2345: Argument of type 'TFunction<"a"[], undefined>' is not assignable// to parameter of type 'TFunction<"a", undefined>'.// Type '"a"[]' is not assignable to type '"a"'.getTitleA[type](tAarray);// TS2345: Argument of type 'TFunction<("a" | "b")[], undefined>' is// not assignable to parameter of type 'TFunction<"a", undefined>'.// Type '("a" | "b")[]' is not assignable to type '"a"'.getTitleA[type](tAB);}returnnull;};
So, only t from useTranslation('a') can be passed to TFunction<'a'>.
Expected behavior
An argument of type TFunction<'a'> should accept an instance of TFunction<'a' | 'b'>.
Your Environment
node: 16.13.0
typescript: 4.4.4
i18next: 21.4.2
react-i18next: 11.14.1
os: Linux
The text was updated successfully, but these errors were encountered:
In my project I temporary add extra declaration, that produce t of type TFunction<'a'> & TFunction<'b'> from useTranslation(['a', 'b']), and so it can be passed to TFunction<'a'> argument.
declare module 'react-i18next'{export*from'react-i18next/';import{i18n}from'i18next';import{Namespace,DefaultNamespace,KeyPrefix,UseTranslationOptions,TFunction,}from'react-i18next/';typeUnionNS<NextendsNamespace>=Nextendsreadonly(infer U)[] ? U : N;typeUnionTFunctions<NAlt,N,TKPrefixextendsKeyPrefix<N>=undefined>=NAltextendsany
? TFunction<NAlt,TKPrefix>
: never;/** * Convert Union to Intersection * * ``` * type C = UnionToIntersection<A | B>; * // A & B * ``` * * @see https://github.com/microsoft/TypeScript/issues/29594 * @see https://github.com/microsoft/TypeScript/issues/29594#issuecomment-477068575 Explanation */typeUnionToIntersection<U>=(Uextendsany ? (u: U)=>void : never)extends(u: infer I,)=>void
? I
: never;exporttypeTFunctionAlt<NextendsNamespace,TKPrefixextendsKeyPrefix<N>=undefined,>=UnionToIntersection<UnionTFunctions<UnionNS<N>,N,TKPrefix>>;typeUseTranslationResponse<NextendsNamespace,TKPrefixextendsKeyPrefix<N>>=[TFunctionAlt<N,TKPrefix>,i18n,boolean,]&{t: TFunctionAlt<N,TKPrefix>;i18n: i18n;ready: boolean;};exportdeclarefunctionuseTranslation<NextendsNamespace=DefaultNamespace,TKPrefixextendsKeyPrefix<N>=undefined,>(ns?: N|Readonly<N>,options?: UseTranslationOptions<N,TKPrefix>,): UseTranslationResponse<N,TKPrefix>;}
🐛 Bug Report
An instance of
TFunction<...>
given fromuseTranslation(['a', 'b'])
cannot be passed as argument toTFunction<'a'>
.To Reproduce
So, only
t
fromuseTranslation('a')
can be passed toTFunction<'a'>
.Expected behavior
An argument of type
TFunction<'a'>
should accept an instance ofTFunction<'a' | 'b'>
.Your Environment
The text was updated successfully, but these errors were encountered: