react-i18next 11.14.1 → 11.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 11.14.2
2
+
3
+ - Add type-safe support to deep keyPrefix [1403](https://github.com/i18next/react-i18next/pull/1403)
4
+
1
5
  ### 11.14.1
2
6
 
3
7
  - Rollback [1402](https://github.com/i18next/react-i18next/pull/1402): Remove generics from Trans component to suppress warning issue [1400](https://github.com/i18next/react-i18next/pull/1400)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-i18next",
3
- "version": "11.14.1",
3
+ "version": "11.14.2",
4
4
  "description": "Internationalization for react done right. Using the i18next i18n ecosystem.",
5
5
  "main": "dist/commonjs/index.js",
6
6
  "types": "./index.d.ts",
package/ts4.1/index.d.ts CHANGED
@@ -102,15 +102,18 @@ type WithOrWithoutPlural<K> = TypeOptions['jsonFormat'] extends 'v4'
102
102
  : K;
103
103
 
104
104
  // Normalize single namespace
105
- type AppendKeys<K1, K2, S extends string = TypeOptions['keySeparator']> = `${K1 & string}${S}${K2 &
106
- string}`;
107
- type AppendKeys2<K1, K2, S extends string = TypeOptions['keySeparator']> = `${K1 &
108
- string}${S}${Exclude<K2, keyof any[]> & string}`;
105
+ export type KeysWithSeparator<K1, K2, S extends string = TypeOptions['keySeparator']> = `${K1 &
106
+ string}${S}${K2 & string}`;
107
+ type KeysWithSeparator2<K1, K2> = KeysWithSeparator<K1, Exclude<K2, keyof any[]>>;
109
108
  type Normalize2<T, K = keyof T> = K extends keyof T
110
109
  ? T[K] extends Record<string, any>
111
110
  ? T[K] extends readonly any[]
112
- ? AppendKeys2<K, WithOrWithoutPlural<keyof T[K]>> | AppendKeys2<K, Normalize2<T[K]>>
113
- : AppendKeys<K, WithOrWithoutPlural<keyof T[K]>> | AppendKeys<K, Normalize2<T[K]>>
111
+ ?
112
+ | KeysWithSeparator2<K, WithOrWithoutPlural<keyof T[K]>>
113
+ | KeysWithSeparator2<K, Normalize2<T[K]>>
114
+ :
115
+ | KeysWithSeparator<K, WithOrWithoutPlural<keyof T[K]>>
116
+ | KeysWithSeparator<K, Normalize2<T[K]>>
114
117
  : never
115
118
  : never;
116
119
  type Normalize<T> = WithOrWithoutPlural<keyof T> | Normalize2<T>;
@@ -167,32 +170,51 @@ type NormalizeMultiReturn<T, V> = V extends `${infer N}:${infer R}`
167
170
  : never
168
171
  : never;
169
172
 
173
+ type NormalizeWithKeyPrefix<
174
+ T,
175
+ K,
176
+ S extends string = TypeOptions['keySeparator']
177
+ > = K extends `${infer K1}${S}${infer K2}`
178
+ ? K1 extends keyof T
179
+ ? NormalizeWithKeyPrefix<T[K1], K2>
180
+ : never
181
+ : K extends keyof T
182
+ ? T[K] extends string
183
+ ? never
184
+ : Normalize<T[K]>
185
+ : never;
186
+
170
187
  type KeyPrefix<N extends Namespace> = N extends keyof DefaultResources
171
- ? Fallback<string, keyof DefaultResources[N]> | undefined
188
+ ? Normalize<DefaultResources[N]> | undefined
172
189
  : string | undefined;
173
190
 
174
191
  export type TFuncKey<
175
192
  N extends Namespace = DefaultNamespace,
176
- TKPrefix extends KeyPrefix<N> = undefined,
193
+ TKPrefix = undefined,
177
194
  T = DefaultResources
178
195
  > = N extends (keyof T)[] | Readonly<(keyof T)[]>
179
196
  ? NormalizeMulti<T, N[number]>
180
197
  : N extends keyof T
181
- ? TKPrefix extends keyof T[N]
182
- ? Normalize<T[N][TKPrefix]>
183
- : Normalize<T[N]>
198
+ ? TKPrefix extends undefined
199
+ ? Normalize<T[N]>
200
+ : NormalizeWithKeyPrefix<T[N], TKPrefix>
184
201
  : string;
185
202
 
186
- export type TFuncReturn<N, TKeys, TDefaultResult, T = DefaultResources> = N extends (keyof T)[]
203
+ export type TFuncReturn<
204
+ N,
205
+ TKeys,
206
+ TDefaultResult,
207
+ TKPrefix = undefined,
208
+ T = DefaultResources
209
+ > = N extends (keyof T)[]
187
210
  ? NormalizeMultiReturn<T, TKeys>
188
211
  : N extends keyof T
189
- ? NormalizeReturn<T[N], TKeys>
212
+ ? TKPrefix extends undefined
213
+ ? NormalizeReturn<T[N], TKeys>
214
+ : NormalizeReturn<T[N], KeysWithSeparator<TKPrefix, TKeys>>
190
215
  : Fallback<TDefaultResult>;
191
216
 
192
- export interface TFunction<
193
- N extends Namespace = DefaultNamespace,
194
- TKPrefix extends KeyPrefix<N> = undefined
195
- > {
217
+ export interface TFunction<N extends Namespace = DefaultNamespace, TKPrefix = undefined> {
196
218
  <
197
219
  TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
198
220
  TDefaultResult extends TFunctionResult = string,
@@ -200,7 +222,7 @@ export interface TFunction<
200
222
  >(
201
223
  key: TKeys | TKeys[],
202
224
  options?: TOptions<TInterpolationMap> | string,
203
- ): TFuncReturn<N, TKeys, TDefaultResult>;
225
+ ): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
204
226
  <
205
227
  TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
206
228
  TDefaultResult extends TFunctionResult = string,
@@ -209,13 +231,13 @@ export interface TFunction<
209
231
  key: TKeys | TKeys[],
210
232
  defaultValue?: string,
211
233
  options?: TOptions<TInterpolationMap> | string,
212
- ): TFuncReturn<N, TKeys, TDefaultResult>;
234
+ ): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
213
235
  }
214
236
 
215
- type I18nKeyType<N extends Namespace> = TFuncKey<N> extends infer A ? A : never;
216
237
  export interface TransProps<
217
- K extends TFuncKey<N> extends infer A ? A : never,
238
+ K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
218
239
  N extends Namespace = DefaultNamespace,
240
+ TKPrefix = undefined,
219
241
  E extends Element = HTMLDivElement
220
242
  > extends React.HTMLProps<E> {
221
243
  children?: React.ReactNode;
@@ -228,27 +250,25 @@ export interface TransProps<
228
250
  parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
229
251
  tOptions?: {};
230
252
  values?: {};
231
- t?: TFunction<N>;
253
+ t?: TFunction<N, TKPrefix>;
232
254
  }
233
255
 
234
256
  export function Trans<
235
- K extends TFuncKey<N> extends infer A ? A : never,
257
+ K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
236
258
  N extends Namespace = DefaultNamespace,
259
+ TKPrefix extends KeyPrefix<N> = undefined,
237
260
  E extends Element = HTMLDivElement
238
- >(props: TransProps<K, N, E>): React.ReactElement;
261
+ >(props: TransProps<K, N, TKPrefix, E>): React.ReactElement;
239
262
 
240
263
  export function useSSR(initialI18nStore: Resource, initialLanguage: string): void;
241
264
 
242
- export interface UseTranslationOptions<
243
- N extends Namespace = DefaultNamespace,
244
- TKPrefix extends KeyPrefix<N> = undefined
245
- > {
265
+ export interface UseTranslationOptions<TKPrefix = undefined> {
246
266
  i18n?: i18n;
247
267
  useSuspense?: boolean;
248
268
  keyPrefix?: TKPrefix;
249
269
  }
250
270
 
251
- type UseTranslationResponse<N extends Namespace, TKPrefix extends KeyPrefix<N>> = [
271
+ type UseTranslationResponse<N extends Namespace, TKPrefix> = [
252
272
  TFunction<N, TKPrefix>,
253
273
  i18n,
254
274
  boolean,
@@ -263,7 +283,7 @@ export function useTranslation<
263
283
  TKPrefix extends KeyPrefix<N> = undefined
264
284
  >(
265
285
  ns?: N | Readonly<N>,
266
- options?: UseTranslationOptions<N, TKPrefix>,
286
+ options?: UseTranslationOptions<TKPrefix>,
267
287
  ): UseTranslationResponse<N, TKPrefix>;
268
288
 
269
289
  // Need to see usage to improve this