react-i18next 11.13.0 → 11.14.3

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,20 @@
1
+ ### 11.14.3
2
+
3
+ - types: remove undefined from conditional type [1410](https://github.com/i18next/react-i18next/pull/1410)
4
+
5
+ ### 11.14.2
6
+
7
+ - Add type-safe support to deep keyPrefix [1403](https://github.com/i18next/react-i18next/pull/1403)
8
+
9
+ ### 11.14.1
10
+
11
+ - 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)
12
+
13
+ ### 11.14.0
14
+
15
+ - Remove generics from Trans component to suppress warning issue [1400](https://github.com/i18next/react-i18next/pull/1400)
16
+ - Add type support to plurals [1399](https://github.com/i18next/react-i18next/pull/1399)
17
+
1
18
  ### 11.13.0
2
19
 
3
20
  - feat(types): add type-safe support to keyPrefix option [1390](https://github.com/i18next/react-i18next/pull/1390)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-i18next",
3
- "version": "11.13.0",
3
+ "version": "11.14.3",
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",
@@ -85,7 +85,7 @@
85
85
  "rollup-plugin-terser": "^5.1.1",
86
86
  "sinon": "^7.2.3",
87
87
  "tslint": "^5.20.1",
88
- "typescript": "^4.3.2",
88
+ "typescript": "^4.5.2",
89
89
  "yargs": "^13.3.0"
90
90
  },
91
91
  "peerDependencies": {
package/ts4.1/index.d.ts CHANGED
@@ -35,8 +35,10 @@ export interface Resources {}
35
35
  * declare module 'react-i18next' {
36
36
  * interface CustomTypeOptions {
37
37
  * defaultNS: 'custom';
38
- * returnNull: false,
39
- * returnEmptyString: false,
38
+ * returnNull: false;
39
+ * returnEmptyString: false;
40
+ * keySeparator: '.';
41
+ * jsonFormat: 'v4';
40
42
  * resources: {
41
43
  * custom: {
42
44
  * foo: 'foo';
@@ -56,6 +58,7 @@ type TypeOptions = MergeBy<
56
58
  returnEmptyString: true;
57
59
  keySeparator: '.';
58
60
  defaultNS: 'translation';
61
+ jsonFormat: 'v4';
59
62
  resources: Resources;
60
63
  },
61
64
  CustomTypeOptions
@@ -92,19 +95,28 @@ declare module 'i18next' {
92
95
  }
93
96
  }
94
97
 
98
+ type WithOrWithoutPlural<K> = TypeOptions['jsonFormat'] extends 'v4'
99
+ ? K extends `${infer B}_${'zero' | 'one' | 'two' | 'few' | 'many' | 'other'}`
100
+ ? B | K
101
+ : K
102
+ : K;
103
+
95
104
  // Normalize single namespace
96
- type AppendKeys<K1, K2, S extends string = TypeOptions['keySeparator']> = `${K1 & string}${S}${K2 &
97
- string}`;
98
- type AppendKeys2<K1, K2, S extends string = TypeOptions['keySeparator']> = `${K1 &
99
- 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[]>>;
100
108
  type Normalize2<T, K = keyof T> = K extends keyof T
101
109
  ? T[K] extends Record<string, any>
102
110
  ? T[K] extends readonly any[]
103
- ? AppendKeys2<K, keyof T[K]> | AppendKeys2<K, Normalize2<T[K]>>
104
- : AppendKeys<K, 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]>>
105
117
  : never
106
118
  : never;
107
- type Normalize<T> = keyof T | Normalize2<T>;
119
+ type Normalize<T> = WithOrWithoutPlural<keyof T> | Normalize2<T>;
108
120
 
109
121
  // Normalize multiple namespaces
110
122
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
@@ -158,32 +170,51 @@ type NormalizeMultiReturn<T, V> = V extends `${infer N}:${infer R}`
158
170
  : never
159
171
  : never;
160
172
 
161
- type KeyPrefix<N extends Namespace> = N extends keyof DefaultResources
162
- ? Fallback<string, keyof DefaultResources[N]> | undefined
163
- : string | undefined;
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
+
187
+ type KeyPrefix<N extends Namespace> =
188
+ | (N extends keyof DefaultResources ? Normalize<DefaultResources[N]> : string)
189
+ | undefined;
164
190
 
165
191
  export type TFuncKey<
166
192
  N extends Namespace = DefaultNamespace,
167
- TKPrefix extends KeyPrefix<N> = undefined,
193
+ TKPrefix = undefined,
168
194
  T = DefaultResources
169
195
  > = N extends (keyof T)[] | Readonly<(keyof T)[]>
170
196
  ? NormalizeMulti<T, N[number]>
171
197
  : N extends keyof T
172
- ? TKPrefix extends keyof T[N]
173
- ? Normalize<T[N][TKPrefix]>
174
- : Normalize<T[N]>
198
+ ? TKPrefix extends undefined
199
+ ? Normalize<T[N]>
200
+ : NormalizeWithKeyPrefix<T[N], TKPrefix>
175
201
  : string;
176
202
 
177
- 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)[]
178
210
  ? NormalizeMultiReturn<T, TKeys>
179
211
  : N extends keyof T
180
- ? NormalizeReturn<T[N], TKeys>
212
+ ? TKPrefix extends undefined
213
+ ? NormalizeReturn<T[N], TKeys>
214
+ : NormalizeReturn<T[N], KeysWithSeparator<TKPrefix, TKeys>>
181
215
  : Fallback<TDefaultResult>;
182
216
 
183
- export interface TFunction<
184
- N extends Namespace = DefaultNamespace,
185
- TKPrefix extends KeyPrefix<N> = undefined
186
- > {
217
+ export interface TFunction<N extends Namespace = DefaultNamespace, TKPrefix = undefined> {
187
218
  <
188
219
  TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
189
220
  TDefaultResult extends TFunctionResult = string,
@@ -191,7 +222,7 @@ export interface TFunction<
191
222
  >(
192
223
  key: TKeys | TKeys[],
193
224
  options?: TOptions<TInterpolationMap> | string,
194
- ): TFuncReturn<N, TKeys, TDefaultResult>;
225
+ ): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
195
226
  <
196
227
  TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
197
228
  TDefaultResult extends TFunctionResult = string,
@@ -200,12 +231,13 @@ export interface TFunction<
200
231
  key: TKeys | TKeys[],
201
232
  defaultValue?: string,
202
233
  options?: TOptions<TInterpolationMap> | string,
203
- ): TFuncReturn<N, TKeys, TDefaultResult>;
234
+ ): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
204
235
  }
205
236
 
206
237
  export interface TransProps<
207
- K extends TFuncKey<N> extends infer A ? A : never,
238
+ K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
208
239
  N extends Namespace = DefaultNamespace,
240
+ TKPrefix = undefined,
209
241
  E extends Element = HTMLDivElement
210
242
  > extends React.HTMLProps<E> {
211
243
  children?: React.ReactNode;
@@ -218,26 +250,25 @@ export interface TransProps<
218
250
  parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
219
251
  tOptions?: {};
220
252
  values?: {};
221
- t?: TFunction<N>;
253
+ t?: TFunction<N, TKPrefix>;
222
254
  }
255
+
223
256
  export function Trans<
224
- K extends TFuncKey<N> extends infer A ? A : never,
257
+ K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
225
258
  N extends Namespace = DefaultNamespace,
259
+ TKPrefix extends KeyPrefix<N> = undefined,
226
260
  E extends Element = HTMLDivElement
227
- >(props: TransProps<K, N, E>): React.ReactElement;
261
+ >(props: TransProps<K, N, TKPrefix, E>): React.ReactElement;
228
262
 
229
263
  export function useSSR(initialI18nStore: Resource, initialLanguage: string): void;
230
264
 
231
- export interface UseTranslationOptions<
232
- N extends Namespace = DefaultNamespace,
233
- TKPrefix extends KeyPrefix<N> = undefined
234
- > {
265
+ export interface UseTranslationOptions<TKPrefix = undefined> {
235
266
  i18n?: i18n;
236
267
  useSuspense?: boolean;
237
268
  keyPrefix?: TKPrefix;
238
269
  }
239
270
 
240
- type UseTranslationResponse<N extends Namespace, TKPrefix extends KeyPrefix<N>> = [
271
+ type UseTranslationResponse<N extends Namespace, TKPrefix> = [
241
272
  TFunction<N, TKPrefix>,
242
273
  i18n,
243
274
  boolean,
@@ -252,7 +283,7 @@ export function useTranslation<
252
283
  TKPrefix extends KeyPrefix<N> = undefined
253
284
  >(
254
285
  ns?: N | Readonly<N>,
255
- options?: UseTranslationOptions<N, TKPrefix>,
286
+ options?: UseTranslationOptions<TKPrefix>,
256
287
  ): UseTranslationResponse<N, TKPrefix>;
257
288
 
258
289
  // Need to see usage to improve this