Skip to content

Commit

Permalink
Parameterized use of TFunction fails while WithT use works (#1188)
Browse files Browse the repository at this point in the history
* Parameterized use of TFunction fails while WithT use works

* rename tests

* use simple type declaration for TFunction - allows for external parameterized use

* fix lint error and add new test with optional arg

* Using typescript scrict, it appears undefined must be in the TResult to play nice with optional args/props
  • Loading branch information
rosskevin committed Jan 23, 2019
1 parent e0fa366 commit aeb1554
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
15 changes: 8 additions & 7 deletions index.d.ts
Expand Up @@ -471,7 +471,7 @@ declare namespace i18next {
// Expose parameterized t in the i18next interface hierarchy
// NOTE: duplicate of TFunction - not sure why I can't find a workable reuse pattern
t<
TResult extends string | object | Array<string | object> = string,
TResult extends string | object | Array<string | object> | undefined = string,
TKeys extends string = string,
TValues extends object = object
>(
Expand All @@ -480,14 +480,15 @@ declare namespace i18next {
): TResult;
}

// NOTE: duplicate of WithT.t - not sure why I can't find a workable reuse pattern
interface TFunction<
TResult extends string | object | Array<string | object> = string,
// NOTE: somewhat duplicate of WithT.t but cannot use interface - not sure why I can't find a workable reuse pattern
type TFunction = <
TResult extends string | object | Array<string | object> | undefined = string,
TKeys extends string = string,
TValues extends object = object
> {
(key: TKeys | TKeys[], options?: TOptions<TValues>): TResult;
}
>(
key: TKeys | TKeys[],
options?: TOptions<TValues>,
) => TResult;

interface Resource {
[language: string]: ResourceLanguage;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -73,7 +73,7 @@
"rollup-plugin-uglify": "1.0.1",
"sinon": "1.17.7",
"tslint": "^5.12.1",
"typescript": "^3.2.2",
"typescript": "^3.2.4",
"watchify": "3.9.0",
"yargs": "6.6.0"
},
Expand Down
13 changes: 0 additions & 13 deletions test/typescript/t.external.ts

This file was deleted.

35 changes: 35 additions & 0 deletions test/typescript/t.tfunction.test.ts
@@ -0,0 +1,35 @@
import i18next from 'i18next';

/**
* Use of the exported TFunction in external utility methods such as
* NamespaceConsumer children t
*/
function childrenNamespacesConsumer(t: i18next.TFunction, i18n: i18next.i18n) {
// sanity first - tests from i18next t.test
const is: string = i18n.t('friend'); // same as <string>
const io: object = i18n.t<object>('friend');
const isa: string[] = i18n.t<string[]>('friend');
const ioa: object[] = i18n.t<object[]>('friend');

// (failing) now try t provided by NamespacesConsumer
const s: string = t('friend'); // same as <string>
const o: object = t<object>('friend');
const sa: string[] = t<string[]>('friend');
const oa: object[] = t<object[]>('friend');
}

function callsMethodWithOptionalArg(t: i18next.TFunction, i18n: i18next.i18n) {
function displayHint(hint?: string) {
return String(hint);
}
displayHint(i18n.t('friend'));
displayHint(t('friend'));
}

function callsMethodWithRequiredArg(t: i18next.TFunction, i18n: i18next.i18n) {
function displayHint(hint: string) {
return String(hint);
}
displayHint(i18n.t('friend'));
displayHint(t('friend'));
}
File renamed without changes.

0 comments on commit aeb1554

Please sign in to comment.