Skip to content

Commit

Permalink
types: t function overloads for call with ns option in combination wi…
Browse files Browse the repository at this point in the history
…th objects
  • Loading branch information
adrai committed Dec 12, 2022
1 parent 1c4ab47 commit a5d4a56
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 22.4.2

- types: t function overloads for call with ns option in combination with objects

## 22.4.1

- types: more t function overloads for call with options argument but without ns value
Expand Down
48 changes: 37 additions & 11 deletions index.d.ts
Expand Up @@ -893,6 +893,43 @@ export interface TFunction<
key: TKeys | TKeys[],
): TFuncReturn<PassedNS, TKeys, TDefaultResult, TKPrefix>;

// with returnDetails: true, returnObjects: true and ns prop in options
<
TKeys extends TFuncKey<Namespace, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
TDefaultResult extends DefaultTFuncReturnWithObject = object,
TInterpolationMap extends object = StringMap,
PassedNS extends Namespace = N extends string ? N : N extends null ? DefaultNamespace : N,
>(
key: TKeys | TKeys[],
options: TOptions<TInterpolationMap> & {
ns: PassedNS;
returnObjects: true;
returnDetails: true;
},
): TFunctionDetailedResult<TFuncReturn<ActualNS, TKeys, TDefaultResult, TKPrefix>>;

// with returnObjects: true and ns prop in options
<
TKeys extends TFuncKey<Namespace, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
TDefaultResult extends DefaultTFuncReturnWithObject = object,
TInterpolationMap extends object = StringMap,
PassedNS extends Namespace = N extends string ? N : N extends null ? DefaultNamespace : N,
>(
key: TKeys | TKeys[],
options: TOptions<TInterpolationMap> & { ns: PassedNS; returnObjects: true },
): TFuncReturn<PassedNS, TKeys, TDefaultResult, TKPrefix>;

// with passed ns prop in options
<
TKeys extends TFuncKey<PassedNS, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
TDefaultResult extends DefaultTFuncReturn = string,
TInterpolationMap extends object = StringMap,
PassedNS extends Namespace = N extends string ? N : N extends null ? DefaultNamespace : N,
>(
key: TKeys | TKeys[],
options: TOptions<TInterpolationMap> & { ns: PassedNS },
): TFuncReturn<PassedNS, TKeys, TDefaultResult, TKPrefix>;

// with returnDetails: true, returnObjects: true
<
TKeys extends TFuncKey<ActualNS, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
Expand Down Expand Up @@ -923,17 +960,6 @@ export interface TFunction<
options: TOptions<TInterpolationMap> & { returnObjects: true },
): TFuncReturn<ActualNS, TKeys, TDefaultResult, TKPrefix>;

// with passed ns prop in options
<
TKeys extends TFuncKey<PassedNS, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
TDefaultResult extends DefaultTFuncReturn = string,
TInterpolationMap extends object = StringMap,
PassedNS extends Namespace = N extends string ? N : N extends null ? DefaultNamespace : N,
>(
key: TKeys | TKeys[],
options: TOptions<TInterpolationMap> & { ns: PassedNS },
): TFuncReturn<PassedNS, TKeys, TDefaultResult, TKPrefix>;

// with options
<
TKeys extends TFuncKey<UsedNS, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
Expand Down
2 changes: 2 additions & 0 deletions test/typescript/custom-types/t.test.ts
Expand Up @@ -42,6 +42,8 @@ function i18nextTUsage() {
i18next.t('alternate:foobar.barfoo');
// i18next.t('alternate:foobar.deep').deeper.deeeeeper; // i18next would say: "key 'foobar.deep (en)' returned an object instead of string."
i18next.t('alternate:foobar.deep', { returnObjects: true }).deeper.deeeeeper;
i18next.t('foobar.deep', { ns: 'alternate', returnObjects: true }).deeper.deeeeeper;
i18next.t('foobar.deep', { ns: 'alternate', returnObjects: true, returnDetails: true }).res;
i18next.t('custom:bar');
i18next.t('bar', { ns: 'custom' });
// @ts-expect-error
Expand Down

0 comments on commit a5d4a56

Please sign in to comment.