-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
index.d.ts
194 lines (169 loc) · 5.24 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import i18next, { ReactOptions, i18n, ThirdPartyModule, Resource, TOptions, StringMap } from 'i18next';
import * as React from 'react';
/**
* 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;
export type Namespace = ResourcesKey | ResourcesKey[];
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<any>;
export function getInitialProps(): {
initialI18nStore: {
[ns: string]: {};
};
initialLanguage: string;
};
export interface ReportNamespaces {
addUsedNamespaces(namespaces: Namespace[]): void;
getUsedNamespaces(): string[];
}
declare module 'i18next' {
interface i18n {
reportNamespaces: ReportNamespaces;
}
}
// Normalize single namespace
type OmitArrayProps<T> = Exclude<T, keyof any[]>;
type AppendKeys<K1, K2> = `${K1 & string}.${OmitArrayProps<K2> & string}`;
type Normalize2<T, K = keyof T> = K extends keyof T
? T[K] extends object
? AppendKeys<K, keyof T[K]> | AppendKeys<K, Normalize2<T[K]>>
: never
: never;
type Normalize<T> = keyof T | Normalize2<T>;
// Normalize multiple namespaces
type UnionToIntersection<U> = (U extends any
? (k: U) => void
: never) extends (k: infer I) => void
? I
: never;
type LastOf<T> = UnionToIntersection<
T extends any ? () => T : never
> extends () => infer R
? R
: never;
type AppendNS<N, K> = `${N & string}:${K & string}`;
type NormalizeMulti<T, U extends keyof T, L = LastOf<U>> = L extends U
? AppendNS<L, Normalize<T[L]>> | NormalizeMulti<T, Exclude<U, L>>
: never;
type NormalizeReturn<T, V> = V extends `${infer K}.${infer R}`
? K extends keyof T
? NormalizeReturn<T[K], R>
: never
: V extends keyof T
? T[V]
: never;
type NormalizeMultiReturn<T, V> = V extends `${infer N}:${infer R}`
? N extends keyof T
? NormalizeReturn<T[N], R>
: never
: never;
export type TFuncKey<N, T = Resources> = N extends (keyof T)[]
? NormalizeMulti<T, N[number]>
: N extends keyof T
? Normalize<T[N]>
: string;
export type TFuncReturn<N, P, T = Resources> = N extends (keyof T)[]
? NormalizeMultiReturn<T, P>
: N extends keyof T
? NormalizeReturn<T[N], P>
: string;
export interface TFunction<N extends Namespace> {
<K extends TFuncKey<N>, TInterpolationMap extends object = StringMap>(
key: K,
options?: TOptions<TInterpolationMap> | string,
): TFuncReturn<N, K>;
<K extends TFuncKey<N>, TInterpolationMap extends object = StringMap>(
key: K,
defaultValue?: string,
options?: TOptions<TInterpolationMap> | string,
): TFuncReturn<N, K>;
}
export interface TransProps<N extends Namespace, K extends TFuncKey<N>, E extends Element = HTMLDivElement>
extends React.HTMLProps<E> {
children?: React.ReactNode;
components?: React.ReactNode[] | { [tagName: string]: React.ReactNode };
count?: number;
defaults?: string;
i18n?: i18n;
i18nKey?: K;
ns?: N;
parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
tOptions?: {};
values?: {};
t?: TFunction<N>;
}
export function Trans<N extends Namespace, K extends TFuncKey<N>, E extends Element = HTMLDivElement>(
props: TransProps<N, K, E>
): React.ReactElement;
export function useSSR(initialI18nStore: Resource, initialLanguage: string): void;
export interface UseTranslationOptions {
i18n?: i18n;
useSuspense?: boolean;
}
type UseTranslationResponse<N extends Namespace> = [TFunction<N>, i18n, boolean] & {
t: TFunction<N>;
i18n: i18n;
ready: boolean;
};
export function useTranslation<
N extends Namespace = Extract<ResourcesKey, 'translation'>
>(
ns?: N,
options?: UseTranslationOptions
): UseTranslationResponse<N>;
// Need to see usage to improve this
export function withSSR(): <Props>(
WrappedComponent: React.ComponentType<Props>,
) => {
({
initialI18nStore,
initialLanguage,
...rest
}: {
initialI18nStore: Resource;
initialLanguage: string;
} & Props): React.FunctionComponentElement<Props>;
getInitialProps: (ctx: unknown) => Promise<any>;
};
export interface WithTranslation<N extends Namespace> {
t: TFunction<N>;
i18n: i18n;
tReady: boolean;
}
export interface WithTranslationProps {
i18n?: i18n;
useSuspense?: boolean;
}
export function withTranslation<N extends Namespace>(
ns?: N,
options?: {
withRef?: boolean;
},
): <P extends WithTranslation<N>>(
component: React.ComponentType<P>,
) => React.ComponentType<Omit<P, keyof WithTranslation<N>> & WithTranslationProps>;
export interface I18nextProviderProps {
i18n: i18n;
defaultNS?: string;
}
export const I18nextProvider: React.FunctionComponent<I18nextProviderProps>;
export const I18nContext: React.Context<{ i18n: i18n }>;
export interface TranslationProps<N extends Namespace> {
children: (
t: TFunction<N>,
options: {
i18n: i18n;
lng: string;
},
ready: boolean,
) => React.ReactNode;
ns?: N;
i18n?: i18n;
}
export function Translation<N extends Namespace>(props: TranslationProps<N>): any;