i18next 26.5.12 → 26.6.0
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/dist/esm/package.json +1 -1
- package/package.json +1 -1
- package/typescript/options.d.ts +9 -0
- package/typescript/t.d.ts +72 -16
package/dist/esm/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"type":"module","version":"26.
|
|
1
|
+
{"type":"module","version":"26.6.0"}
|
package/package.json
CHANGED
package/typescript/options.d.ts
CHANGED
|
@@ -159,6 +159,15 @@ export type TypeOptions = $MergeBy<
|
|
|
159
159
|
*/
|
|
160
160
|
strictKeyChecks: false;
|
|
161
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Flag that makes the `t()` options required for a string with interpolation
|
|
164
|
+
* variables, so that `{{name}}` cannot be left out of a call. Applies to the
|
|
165
|
+
* string-key API (with `strictKeyChecks`, also to `t(key, defaultValue)`) and
|
|
166
|
+
* to the selector API. Messages under `parseInterpolation: 'icu'` or `'mf2'`
|
|
167
|
+
* require their arguments regardless of this flag.
|
|
168
|
+
*/
|
|
169
|
+
strictInterpolationChecks: false;
|
|
170
|
+
|
|
162
171
|
/**
|
|
163
172
|
* Prefix for interpolation
|
|
164
173
|
*/
|
package/typescript/t.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ type _InterpolationSuffix = TypeOptions['interpolationSuffix'];
|
|
|
34
34
|
type _UnescapePrefix = TypeOptions['unescapePrefix'];
|
|
35
35
|
type _UnescapeSuffix = TypeOptions['unescapeSuffix'];
|
|
36
36
|
type _StrictKeyChecks = TypeOptions['strictKeyChecks'];
|
|
37
|
+
type _StrictInterpolationChecks = TypeOptions['strictInterpolationChecks'];
|
|
37
38
|
type _EnableSelector = TypeOptions['enableSelector'];
|
|
38
39
|
type _ParseInterpolation = TypeOptions['parseInterpolation'];
|
|
39
40
|
type _InterpolationFormatTypeMap = TypeOptions['interpolationFormatTypeMap'];
|
|
@@ -606,18 +607,37 @@ type EffectiveKPrefix<KPrefix, TOpt> = TOpt extends { keyPrefix: infer OptKP ext
|
|
|
606
607
|
|
|
607
608
|
/** `parseInterpolation: 'icu'` or `'mf2'`: messages are typed by their arguments. */
|
|
608
609
|
type _ParsesMessageFormat = [_ParseInterpolation] extends ['icu'] | ['mf2'] ? true : false;
|
|
610
|
+
/**
|
|
611
|
+
* Whether `t()` options are required for a message with variables: always under
|
|
612
|
+
* ICU / MF2 (on a missing one, i18next-icu renders the raw message and
|
|
613
|
+
* i18next-mf2 a `{$name}` fallback), and with `strictInterpolationChecks: true`
|
|
614
|
+
* for `{{variables}}` (i18next renders the raw placeholder).
|
|
615
|
+
*/
|
|
616
|
+
type _RequiresMessageOptions = _ParsesMessageFormat extends true
|
|
617
|
+
? true
|
|
618
|
+
: [_ParseInterpolation] extends [true]
|
|
619
|
+
? _StrictInterpolationChecks extends true
|
|
620
|
+
? true
|
|
621
|
+
: false
|
|
622
|
+
: false;
|
|
609
623
|
|
|
610
624
|
/**
|
|
611
|
-
* `t()` options are optional, except for a message with
|
|
612
|
-
* `
|
|
613
|
-
*
|
|
614
|
-
* resolves eagerly; a generic one here would break inference.
|
|
625
|
+
* `t()` options are optional, except for a message with variables when
|
|
626
|
+
* `_RequiresMessageOptions` is true. The conditional resolves eagerly; a generic
|
|
627
|
+
* one here would break inference.
|
|
615
628
|
*/
|
|
616
|
-
type _KeyAndOptions<Key, Ret, Opts> =
|
|
629
|
+
type _KeyAndOptions<Key, Ret, Opts> = _RequiresMessageOptions extends true
|
|
617
630
|
? [key: Key | Key[], options: Opts] | [key: (Key | Key[]) & _IfOptionalOptions<Ret>]
|
|
618
631
|
: [key: Key | Key[], options?: Opts];
|
|
632
|
+
/**
|
|
633
|
+
* `t(key, defaultValue)` under `strictKeyChecks`: the variables are required only
|
|
634
|
+
* with `strictInterpolationChecks`, else `unknown` (the call stays as before).
|
|
635
|
+
*/
|
|
636
|
+
type _IfOptionalDefaultOptions<Ret> = _StrictInterpolationChecks extends true
|
|
637
|
+
? _IfOptionalOptions<Ret>
|
|
638
|
+
: unknown;
|
|
619
639
|
/** `unknown` when options may be left out, else a type naming the missing arguments. */
|
|
620
|
-
type _IfOptionalOptions<Ret> =
|
|
640
|
+
type _IfOptionalOptions<Ret> = _RequiresMessageOptions extends true
|
|
621
641
|
? {} extends InterpolationMap<Ret>
|
|
622
642
|
? unknown
|
|
623
643
|
: { 'pass these arguments as t() options': InterpolationMap<Ret> }
|
|
@@ -625,6 +645,25 @@ type _IfOptionalOptions<Ret> = _ParsesMessageFormat extends true
|
|
|
625
645
|
/** Options parameter of a selector overload: required for a message with arguments. */
|
|
626
646
|
type _SelectorOptionsArg<Ret, Opts> =
|
|
627
647
|
unknown extends _IfOptionalOptions<Ret> ? [options?: Opts] : [options: Opts];
|
|
648
|
+
/** Union of the values an array of selectors resolves to (plural brands kept). */
|
|
649
|
+
type _SelectorsReturn<Fns extends readonly unknown[]> = Fns[number] extends (
|
|
650
|
+
...args: any[]
|
|
651
|
+
) => infer R
|
|
652
|
+
? R
|
|
653
|
+
: never;
|
|
654
|
+
/** Whether any member of the union is a plural group (`false` for `any`). */
|
|
655
|
+
type _HasPlural<R> = 0 extends 1 & R
|
|
656
|
+
? false
|
|
657
|
+
: [Extract<R, PluralValue<string>>] extends [never]
|
|
658
|
+
? false
|
|
659
|
+
: true;
|
|
660
|
+
/** `{ count: number }` when any member of the union is a plural group. */
|
|
661
|
+
type _CountIfPlural<R> = _HasPlural<R> extends true ? { count: number } : {};
|
|
662
|
+
/** Options of a selector-array overload: required for variables or a plural member. */
|
|
663
|
+
type _SelectorArrayOptionsArg<R, Opts> =
|
|
664
|
+
_HasPlural<R> extends true
|
|
665
|
+
? [options: Opts & { count: number } & InterpolationMap<DeepUnwrapPlural<R>>]
|
|
666
|
+
: _SelectorOptionsArg<DeepUnwrapPlural<R>, Opts & InterpolationMap<DeepUnwrapPlural<R>>>;
|
|
628
667
|
|
|
629
668
|
/** ************************
|
|
630
669
|
* T function declaration *
|
|
@@ -663,7 +702,7 @@ interface TFunctionStrict<
|
|
|
663
702
|
>(
|
|
664
703
|
key: Key | Key[],
|
|
665
704
|
defaultValue: string,
|
|
666
|
-
options
|
|
705
|
+
options: TOpt &
|
|
667
706
|
InterpolationMap<Ret> & {
|
|
668
707
|
context?: Key extends string
|
|
669
708
|
? unknown extends TOpt['context']
|
|
@@ -672,6 +711,14 @@ interface TFunctionStrict<
|
|
|
672
711
|
: never;
|
|
673
712
|
},
|
|
674
713
|
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
714
|
+
<
|
|
715
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
716
|
+
const TOpt extends TOptions,
|
|
717
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
718
|
+
>(
|
|
719
|
+
key: (Key | Key[]) & _IfOptionalDefaultOptions<$NoInfer<Ret>>,
|
|
720
|
+
defaultValue: string,
|
|
721
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
675
722
|
}
|
|
676
723
|
|
|
677
724
|
interface TFunctionNonStrict<
|
|
@@ -826,7 +873,7 @@ interface TFunctionSelector<Ns extends Namespace, KPrefix, Source> extends Brand
|
|
|
826
873
|
NewSrc extends GetSource<NewNs, KPrefix>,
|
|
827
874
|
>(
|
|
828
875
|
selectors: readonly SelectorFn<NewSrc, ApplyTarget<Target, Opts>, Opts>[],
|
|
829
|
-
options: Opts & InterpolationMap<Target> & { ns: NewNs }
|
|
876
|
+
options: Opts & InterpolationMap<Target> & { ns: NewNs } & _CountIfPlural<Target>,
|
|
830
877
|
): SelectorReturn<Target, Opts>;
|
|
831
878
|
|
|
832
879
|
// ── Single selector with explicit `ns` ──────────────────────────────────────
|
|
@@ -843,6 +890,21 @@ interface TFunctionSelector<Ns extends Namespace, KPrefix, Source> extends Brand
|
|
|
843
890
|
} & (Target extends PluralValue<string> ? { count: number } : {}),
|
|
844
891
|
): SelectorReturn<Target, Opts>;
|
|
845
892
|
|
|
893
|
+
// ── Array of selectors with defaultValue — preserves literal type of DV ─────
|
|
894
|
+
// Same TypeScript limitation as the single-selector defaultValue overload below:
|
|
895
|
+
// `const Opts` loses the `defaultValue` literal when inferred through the
|
|
896
|
+
// conditional rest tuple of the general array overload. A plural member
|
|
897
|
+
// requires `count`, as in the single-selector overload.
|
|
898
|
+
<
|
|
899
|
+
const Fns extends readonly ((src: Select<Source, Opts['context']>) => string | object)[],
|
|
900
|
+
const DV extends string,
|
|
901
|
+
const Opts extends SelectorOptions<Ns[number]> = SelectorOptions<Ns[number]>,
|
|
902
|
+
>(
|
|
903
|
+
selectors: Fns,
|
|
904
|
+
options: Opts & { defaultValue: DV } & _CountIfPlural<_SelectorsReturn<Fns>> &
|
|
905
|
+
InterpolationMap<DeepUnwrapPlural<_SelectorsReturn<Fns>>>,
|
|
906
|
+
): SelectorReturn<_SelectorsReturn<Fns>, Opts, DV>;
|
|
907
|
+
|
|
846
908
|
// ── Array of selectors with default `ns` ─────────────────────────────────────
|
|
847
909
|
// Captures the selector tuple as `const Fns` so TypeScript preserves each
|
|
848
910
|
// element's exact return type. The union of return types is then extracted
|
|
@@ -854,14 +916,8 @@ interface TFunctionSelector<Ns extends Namespace, KPrefix, Source> extends Brand
|
|
|
854
916
|
const Opts extends SelectorOptions<Ns[number]> = SelectorOptions<Ns[number]>,
|
|
855
917
|
>(
|
|
856
918
|
selectors: Fns,
|
|
857
|
-
...args:
|
|
858
|
-
|
|
859
|
-
Opts &
|
|
860
|
-
InterpolationMap<
|
|
861
|
-
DeepUnwrapPlural<Fns[number] extends (...args: any[]) => infer R ? R : never>
|
|
862
|
-
>
|
|
863
|
-
>
|
|
864
|
-
): SelectorReturn<Fns[number] extends (...args: any[]) => infer R ? R : never, Opts>;
|
|
919
|
+
...args: _SelectorArrayOptionsArg<_SelectorsReturn<Fns>, Opts>
|
|
920
|
+
): SelectorReturn<_SelectorsReturn<Fns>, Opts>;
|
|
865
921
|
|
|
866
922
|
// ── Single selector with context + returnObjects ────────────────────────────
|
|
867
923
|
// More specific than the general context overload below. Captures `Context` as
|