import i18next, { ReactOptions, i18n, ThirdPartyModule, Resource, TOptions, StringMap, TFunctionResult, } from 'i18next'; import * as React from 'react'; type Subtract = Omit; /** * Due to a limitation/bug on typescript 4.1 (https://github.com/microsoft/TypeScript/issues/41406), we added * "extends infer A ? A : never" in a few places to suppress the error "Type instantiation is excessively deep and possibly infinite." * on cases where users have more than 22 namespaces. Once the issue is fixed, we can remove all instances of the workaround used. * * Reference of the bug reported: https://github.com/i18next/react-i18next/issues/1222 */ /** * This interface can be augmented by users to add types to `react-i18next` default resources. * * @deprecated use the `resources` key of `CustomTypeOptions` instead */ export interface Resources {} /** * This interface can be augmented by users to add types to `react-i18next`. It accepts a `defaultNS`, `resources`, `returnNull` and `returnEmptyString` properties. * * Usage: * ```ts * // react-i18next.d.ts * import 'react-i18next'; * declare module 'react-i18next' { * interface CustomTypeOptions { * defaultNS: 'custom'; * returnNull: false; * returnEmptyString: false; * nsSeparator: ':'; * keySeparator: '.'; * jsonFormat: 'v4'; * allowObjectInHTMLChildren: false; * resources: { * custom: { * foo: 'foo'; * }; * }; * } * } * ``` */ export interface CustomTypeOptions {} type MergeBy = Omit & K; type TypeOptions = MergeBy< { returnNull: true; returnEmptyString: true; keySeparator: '.'; nsSeparator: ':'; defaultNS: 'translation'; jsonFormat: 'v4'; resources: Resources; allowObjectInHTMLChildren: false; }, CustomTypeOptions >; type DefaultResources = TypeOptions['resources']; type DefaultNamespace = T extends Fallback ? T : string; type Fallback = [T] extends [never] ? F : T; export type Namespace> = F | F[]; export function setDefaults(options: ReactOptions): void; export function getDefaults(): ReactOptions; export function setI18n(instance: i18n): void; export function getI18n(): i18n; export const initReactI18next: ThirdPartyModule; export function composeInitialProps(ForComponent: any): (ctx: unknown) => Promise; export function getInitialProps(): { initialI18nStore: { [ns: string]: {}; }; initialLanguage: string; }; export interface ReportNamespaces { addUsedNamespaces(namespaces: Namespace[]): void; getUsedNamespaces(): string[]; } declare module 'i18next' { interface i18n { reportNamespaces: ReportNamespaces; } } type ObjectOrNever = TypeOptions['allowObjectInHTMLChildren'] extends true ? Record : never; type ReactI18NextChild = React.ReactNode | ObjectOrNever; declare module 'react' { interface HTMLAttributes { children?: ReactI18NextChild | Iterable; } } type PluralSuffix = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'; type WithOrWithoutPlural = TypeOptions['jsonFormat'] extends 'v4' ? K extends `${infer B}_${PluralSuffix}` ? B | K : K : K; // Normalize single namespace export type KeysWithSeparator = `${K1 & string}${S}${K2 & string}`; type KeysWithSeparator2 = KeysWithSeparator>; type Normalize2 = K extends keyof T ? T[K] extends Record ? T[K] extends readonly any[] ? | KeysWithSeparator2> | KeysWithSeparator2> : | KeysWithSeparator> | KeysWithSeparator> : never : never; type Normalize = WithOrWithoutPlural | Normalize2; // Normalize multiple namespaces type KeyWithNSSeparator = `${N & string}${S}${K & string}`; type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; type LastOf = UnionToIntersection T : never> extends () => infer R ? R : never; type NormalizeMulti> = L extends U ? KeyWithNSSeparator> | NormalizeMulti> : never; interface CustomTypeParameters { returnNull?: boolean; returnEmptyString?: boolean; } type TypeOptionsFallback = Option extends false ? TranslationValue extends MatchingValue ? string : TranslationValue : TranslationValue; /** * Checks if user has enabled `returnEmptyString` and `returnNull` options to retrieve correct values. */ export type NormalizeByTypeOptions< TranslationValue, Options extends CustomTypeParameters = TypeOptions, R = TypeOptionsFallback > = TypeOptionsFallback; type StringIfPlural = TypeOptions['jsonFormat'] extends 'v4' ? T extends `${string}_${PluralSuffix}` ? string : never : never; type NormalizeReturn< T, V, S extends string | false = TypeOptions['keySeparator'] > = V extends keyof T ? NormalizeByTypeOptions : S extends false ? V : V extends `${infer K}${S}${infer R}` ? K extends keyof T ? NormalizeReturn : never : StringIfPlural; type NormalizeMultiReturn = V extends `${infer N}:${infer R}` ? N extends keyof T ? NormalizeReturn : never : never; type NormalizeWithKeyPrefix< T, K, S extends string = TypeOptions['keySeparator'] > = K extends `${infer K1}${S}${infer K2}` ? K1 extends keyof T ? NormalizeWithKeyPrefix : never : K extends keyof T ? T[K] extends string ? never : Normalize : never; type KeyPrefix = | (N extends keyof DefaultResources ? Normalize : string) | undefined; export type TFuncKey< N extends Namespace = DefaultNamespace, TKPrefix = undefined, T = DefaultResources > = N extends (keyof T)[] | Readonly<(keyof T)[]> ? NormalizeMulti : N extends keyof T ? TKPrefix extends undefined ? Normalize : NormalizeWithKeyPrefix : string; export type TFuncReturn< N, TKeys, TDefaultResult, TKPrefix = undefined, T = DefaultResources > = N extends (keyof T)[] ? NormalizeMultiReturn : N extends keyof T ? TKPrefix extends undefined ? NormalizeReturn : NormalizeReturn> : Fallback; export interface TFunction { < TKeys extends TFuncKey | TemplateStringsArray extends infer A ? A : never, TDefaultResult extends TFunctionResult | React.ReactNode = string, TInterpolationMap extends object = StringMap >( key: TKeys | TKeys[], options?: TOptions | string, ): TFuncReturn; < TKeys extends TFuncKey | TemplateStringsArray extends infer A ? A : never, TDefaultResult extends TFunctionResult | React.ReactNode = string, TInterpolationMap extends object = StringMap >( key: TKeys | TKeys[], defaultValue?: string, options?: TOptions | string, ): TFuncReturn; } type TransChild = React.ReactNode | Record; export type TransProps< K extends TFuncKey extends infer A ? A : never, N extends Namespace = DefaultNamespace, TKPrefix = undefined, E = React.HTMLProps > = E & { children?: TransChild | TransChild[]; components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement }; count?: number; context?: string; defaults?: string; i18n?: i18n; i18nKey?: K | K[]; ns?: N; parent?: string | React.ComponentType | null; // used in React.createElement if not null tOptions?: {}; values?: {}; shouldUnescape?: boolean; t?: TFunction; }; export function Trans< K extends TFuncKey extends infer A ? A : never, N extends Namespace = DefaultNamespace, TKPrefix extends KeyPrefix = undefined, E = React.HTMLProps >(props: TransProps): React.ReactElement; export function useSSR(initialI18nStore: Resource, initialLanguage: string): void; export interface UseTranslationOptions { i18n?: i18n; useSuspense?: boolean; keyPrefix?: TKPrefix; bindI18n?: string | false; nsMode?: 'fallback' | 'default'; } export type UseTranslationResponse = [ TFunction, i18n, boolean, ] & { t: TFunction; i18n: i18n; ready: boolean; }; export function useTranslation< N extends Namespace = DefaultNamespace, TKPrefix extends KeyPrefix = undefined >( ns?: N | Readonly, options?: UseTranslationOptions, ): UseTranslationResponse; // Need to see usage to improve this export function withSSR(): ( WrappedComponent: React.ComponentType, ) => { ({ initialI18nStore, initialLanguage, ...rest }: { initialI18nStore: Resource; initialLanguage: string; } & Props): React.FunctionComponentElement; getInitialProps: (ctx: unknown) => Promise; }; export interface WithTranslation< N extends Namespace = DefaultNamespace, TKPrefix extends KeyPrefix = undefined > { t: TFunction; i18n: i18n; tReady: boolean; } export interface WithTranslationProps { i18n?: i18n; useSuspense?: boolean; } export function withTranslation< N extends Namespace = DefaultNamespace, TKPrefix extends KeyPrefix = undefined >( ns?: N, options?: { withRef?: boolean; keyPrefix?: TKPrefix; }, ): < C extends React.ComponentType & WithTranslationProps>, ResolvedProps = JSX.LibraryManagedAttributes< C, Subtract, WithTranslationProps> > >( component: C, ) => React.ComponentType> & WithTranslationProps>; export interface I18nextProviderProps { children?: React.ReactNode; i18n: i18n; defaultNS?: string; } export const I18nextProvider: React.FunctionComponent; export const I18nContext: React.Context<{ i18n: i18n }>; export interface TranslationProps< N extends Namespace = DefaultNamespace, TKPrefix extends KeyPrefix = undefined > { children: ( t: TFunction, options: { i18n: i18n; lng: string; }, ready: boolean, ) => React.ReactNode; ns?: N; i18n?: i18n; useSuspense?: boolean; keyPrefix?: TKPrefix; nsMode?: 'fallback' | 'default'; } export function Translation< N extends Namespace = DefaultNamespace, TKPrefix extends KeyPrefix = undefined >(props: TranslationProps): any;