Skip to content
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

Add workaround to suppress infinite instantiation warning #1227

Merged
merged 3 commits into from
Dec 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/ts4.1/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import i18next, {
} from 'i18next';
import * as React from 'react';

/**
* 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.
*/
export interface Resources {}

type ResourcesKey<T = keyof Resources> = [T] extends [never] ? string : T;
type Fallback<F, T = keyof Resources> = [T] extends [never] ? F : T;

export type Namespace = ResourcesKey | ResourcesKey[];
export type Namespace<F = Fallback<string>> = F | F[];

export function setDefaults(options: ReactOptions): void;
export function getDefaults(): ReactOptions;
Expand Down Expand Up @@ -84,25 +92,23 @@ export type TFuncKey<N, T = Resources> = N extends (keyof T)[]
? Normalize<T[N]>
: string;

export type TFuncReturn<N, TKeys, TDefaultResult, T = Resources> = [keyof Resources] extends [never]
? TDefaultResult
: N extends (keyof T)[]
export type TFuncReturn<N, TKeys, TDefaultResult, T = Resources> = N extends (keyof T)[]
? NormalizeMultiReturn<T, TKeys>
: N extends keyof T
? NormalizeReturn<T[N], TKeys>
: string;
: Fallback<TDefaultResult>;

export interface TFunction<N extends Namespace = DefaultNamespace> {
<
TKeys extends TFuncKey<N> | TemplateStringsArray,
TKeys extends TFuncKey<N> | TemplateStringsArray extends infer A ? A : never,
TDefaultResult extends TFunctionResult = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> | string,
): TFuncReturn<N, TKeys, TDefaultResult>;
<
TKeys extends TFuncKey<N> | TemplateStringsArray,
TKeys extends TFuncKey<N> | TemplateStringsArray extends infer A ? A : never,
TDefaultResult extends TFunctionResult = string,
TInterpolationMap extends object = StringMap
>(
Expand All @@ -113,7 +119,7 @@ export interface TFunction<N extends Namespace = DefaultNamespace> {
}

export interface TransProps<
K extends TFuncKey<N>,
K extends TFuncKey<N> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
E extends Element = HTMLDivElement
> extends React.HTMLProps<E> {
Expand All @@ -130,7 +136,7 @@ export interface TransProps<
t?: TFunction<N>;
}
export function Trans<
K extends TFuncKey<N>,
K extends TFuncKey<N> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
E extends Element = HTMLDivElement
>(props: TransProps<K, N, E>): React.ReactElement;
Expand All @@ -148,7 +154,7 @@ type UseTranslationResponse<N extends Namespace> = [TFunction<N>, i18n, boolean]
ready: boolean;
};

type DefaultNamespace<T = 'translation'> = ResourcesKey extends T ? T : string;
type DefaultNamespace<T = 'translation'> = Fallback<string> extends T ? T : string;

export function useTranslation<N extends Namespace = DefaultNamespace>(
ns?: N,
Expand Down