Skip to content

Commit

Permalink
Pull up WithT interface allowing for overrides (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkow authored and rosskevin committed Jan 17, 2019
1 parent 0c60262 commit 140934b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
18 changes: 12 additions & 6 deletions index.d.ts
Expand Up @@ -468,6 +468,17 @@ declare namespace i18next {
(key: TKeys | TKeys[], options?: TranslationOptions<TValues>): TResult
}

/**
* WARNING: Order with generic type parametes are defferent from TranslationFunction for usability
* see: https://github.com/i18next/react-i18next/issues/662
*/
interface WithT{
/**
* Please have a look at the translation functions like interpolation, formatting and plurals for more details on using it.
*/
t<TKeys extends string = string, TValues extends object = object, TResult = any> (key: TKeys | TKeys[], options?: TranslationOptions<TValues>) : TResult;
}

interface Resource {
[language: string]: ResourceLanguage
}
Expand Down Expand Up @@ -566,7 +577,7 @@ declare namespace i18next {
error(...args: any[]): void
}

interface i18n {
interface i18n extends WithT {
/**
* The default export of the i18next module is an i18next instance ready to be initialized by calling init.
* You can create additional instances using the createInstance function.
Expand All @@ -590,11 +601,6 @@ declare namespace i18next {
*/
services: Services

/**
* Please have a look at the translation functions like interpolation, formatting and plurals for more details on using it.
*/
t: TranslationFunction

/**
* Uses the same resolve functionality as the t function and returns true if a key exists.
*/
Expand Down
35 changes: 31 additions & 4 deletions test/typescript/i18next-tests.ts
Expand Up @@ -520,7 +520,34 @@ const t4: i18next.TranslationFunction<string, object, KeyList> = (
options?: i18next.TranslationOptions,
) => ''

i18next.exists('friend')
i18next.exists(['friend', 'tree'])
i18next.exists('friend', { myVar: 'someValue' })
i18next.exists(['friend', 'tree'], { myVar: 'someValue' })
i18next.exists('friend');
i18next.exists(['friend', 'tree']);
i18next.exists('friend', { myVar: 'someValue' });
i18next.exists(['friend', 'tree'], { myVar: 'someValue' });

i18next.t<KeyList>("friend", { myVar: "someValue" });
i18next.t<KeyList>(["friend", "tree"], { myVar: "someValue" });
i18next.t<KeyList, {myVar: "someValue"}>("friend", { myVar: "someValue" });
i18next.t<KeyList, {myVar: "someValue"}>(["friend", "tree"], { myVar: "someValue" });

// NOTION: disable no-unnecessary-generics for generic pattern test.
/* tslint:disable:no-unnecessary-generics */
interface ExWithT extends i18next.WithT {
t<Keys extends KeyList= KeyList, Val extends object= object, R= string>(keys: Keys|Keys[], options?: i18next.TranslationOptions<Val>): R;
t<Keys extends OtherKeyList= OtherKeyList, Val extends object= object, R= string>(keys: Keys|Keys[], options?: i18next.TranslationOptions<Val>): R;
t<Keys extends string= KeyList, R= string>(keys: Keys|Keys[]): R;
}

type OtherKeyList = "private" | "public";

(i18next as ExWithT).t("friend");
(i18next as ExWithT).t("tree");
(i18next as ExWithT).t("private");
(i18next as ExWithT).t("public");
(i18next as ExWithT).t("friend", {});
(i18next as ExWithT).t("private", {});
(i18next as ExWithT).t<KeyList, {myVar: "someValue"}>("friend", {myVar: "someValue"});
(i18next as ExWithT).t<OtherKeyList, {myVar: "someValue"}>("private", {myVar: "someValue"});
const result = (i18next as ExWithT).t<KeyList, {myVar: "someValue"}, {result: 'result'}>("friend", {myVar: "someValue"});
type Check<T extends {result: 'result'}> = T;
type ExWithTResult = Check<typeof result>;

0 comments on commit 140934b

Please sign in to comment.