i18next 25.10.3 → 25.10.5

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.
@@ -1 +1 @@
1
- {"type":"module","version":"25.10.3"}
1
+ {"type":"module","version":"25.10.5"}
package/index.d.ts CHANGED
@@ -11,8 +11,15 @@ import type {
11
11
  ResourceKey,
12
12
  ResourceLanguage,
13
13
  TOptions,
14
+ TypeOptions,
14
15
  } from './typescript/options.js';
15
- import type { KeyPrefix, TFunction, KeyFromSelectorFn } from './typescript/t.js';
16
+ import type {
17
+ KeyPrefix,
18
+ KeyPrefixSelector,
19
+ TFunction,
20
+ KeyFromSelectorFn,
21
+ SelectorKey,
22
+ } from './typescript/t.js';
16
23
 
17
24
  export interface WithT<Ns extends Namespace = DefaultNamespace> {
18
25
  // Expose parameterized t in the i18next interface hierarchy
@@ -200,11 +207,13 @@ export type Callback = (error: any, t: TFunction) => void;
200
207
 
201
208
  /**
202
209
  * Uses similar args as the t function and returns true if a key exists.
210
+ * Acts as a type guard, narrowing the key to {@link SelectorKey} so it can be passed to `t()`.
203
211
  */
204
212
  export interface ExistsFunction<
205
213
  TKeys extends string = string,
206
214
  TInterpolationMap extends object = $Dictionary,
207
215
  > {
216
+ (key: TKeys, options?: TOptions<TInterpolationMap>): key is TKeys & SelectorKey;
208
217
  (key: TKeys | TKeys[], options?: TOptions<TInterpolationMap>): boolean;
209
218
  }
210
219
 
@@ -286,6 +295,18 @@ export interface i18n extends CustomInstanceExtensions {
286
295
  *
287
296
  * Accepts optional keyPrefix that will be automatically applied to returned t function.
288
297
  */
298
+ getFixedT<
299
+ Ns extends Namespace | null,
300
+ const TKPrefixFn extends TypeOptions['enableSelector'] extends true | 'optimize'
301
+ ? KeyPrefixSelector<ActualNs>
302
+ : never,
303
+ ActualNs extends Namespace = Ns extends null ? DefaultNamespace : Ns,
304
+ >(
305
+ lng: string | readonly string[] | null,
306
+ ns: Ns,
307
+ keyPrefix: TKPrefixFn,
308
+ ): TFunction<ActualNs, TKPrefixFn>;
309
+
289
310
  getFixedT<
290
311
  Ns extends Namespace | null = DefaultNamespace,
291
312
  TKPrefix extends KeyPrefix<ActualNs> = undefined,
@@ -560,6 +581,8 @@ export type {
560
581
  TFunctionReturn,
561
582
  TFunctionDetailedResult,
562
583
  KeyPrefix,
584
+ KeyPrefixSelector,
585
+ NsResource,
563
586
  InterpolationMap,
564
587
  SelectorKey,
565
588
  } from './typescript/t.js';
package/jsr.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@i18next/i18next",
3
- "version": "25.10.3",
3
+ "version": "25.10.5",
4
4
  "license": "MIT",
5
5
  "exports": "./src/index.js",
6
6
  "publish": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next",
3
- "version": "25.10.3",
3
+ "version": "25.10.5",
4
4
  "description": "i18next internationalization framework",
5
5
  "main": "./dist/cjs/i18next.js",
6
6
  "module": "./dist/esm/i18next.js",
package/typescript/t.d.ts CHANGED
@@ -464,6 +464,14 @@ export interface TFunction<
464
464
 
465
465
  export type KeyPrefix<Ns extends Namespace> = ResourceKeys<true>[$FirstNamespace<Ns>] | undefined;
466
466
 
467
+ /** The raw (unfiltered) resource object for a given namespace. */
468
+ export type NsResource<Ns extends Namespace> = Ns extends readonly [keyof Resources, any, ...any]
469
+ ? Resources[Ns[0]] & PickNamespaces<Resources, Ns[number]>
470
+ : Resources[$FirstNamespace<Ns>];
471
+
472
+ /** A selector function that can be used as `keyPrefix` to scope `t()` to a sub-tree of the resource. */
473
+ export type KeyPrefixSelector<Ns extends Namespace> = (src: NsResource<Ns>) => object;
474
+
467
475
  /// ////////////// ///
468
476
  /// ↆ selector ↆ ///
469
477
  /// ////////////// ///
@@ -644,17 +652,22 @@ type PickNamespaces<T, K extends keyof any> = {
644
652
  [P in K as P extends keyof T ? P : never]: T[P & keyof T];
645
653
  };
646
654
 
647
- type GetSource<
648
- Ns extends Namespace,
649
- KPrefix,
650
- Res = Ns extends readonly [keyof Resources, any, ...any]
651
- ? Resources[Ns[0]] & PickNamespaces<Resources, Ns[number]>
652
- : Resources[$FirstNamespace<Ns>],
653
- > = KPrefix extends keyof Res
654
- ? Res[KPrefix]
655
- : undefined extends KPrefix
656
- ? Res
657
- : ApplyKeyPrefix<[Res], KPrefix>;
655
+ /** Extracts the sub-tree returned by a selector function used as keyPrefix. */
656
+ type SelectorReturnSource<KPrefix, Fallback> = KPrefix extends (...args: any[]) => infer R
657
+ ? R extends object
658
+ ? R
659
+ : Fallback
660
+ : Fallback;
661
+
662
+ type GetSource<Ns extends Namespace, KPrefix, Res = NsResource<Ns>> = KPrefix extends (
663
+ ...args: any[]
664
+ ) => any
665
+ ? SelectorReturnSource<KPrefix, Res>
666
+ : KPrefix extends keyof Res
667
+ ? Res[KPrefix]
668
+ : undefined extends KPrefix
669
+ ? Res
670
+ : ApplyKeyPrefix<[Res], KPrefix>;
658
671
 
659
672
  type Select<T, Context> = $IsResourcesDefined extends false
660
673
  ? $Turtles