Skip to content

Commit

Permalink
TFunction external use broken without using interface (#1186)
Browse files Browse the repository at this point in the history
* Make external use of TFunction type behave

* Comment this pattern applies to NamespacesConsumer

i18next/react-i18next#693
  • Loading branch information
rosskevin committed Jan 23, 2019
1 parent 537b1d6 commit e0fa366
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.d.ts
Expand Up @@ -469,6 +469,7 @@ declare namespace i18next {

interface WithT {
// 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,
TKeys extends string = string,
Expand All @@ -479,7 +480,14 @@ declare namespace i18next {
): TResult;
}

type TFunction = WithT['t'];
// 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,
TKeys extends string = string,
TValues extends object = object
> {
(key: TKeys | TKeys[], options?: TOptions<TValues>): TResult;
}

interface Resource {
[language: string]: ResourceLanguage;
Expand Down
13 changes: 13 additions & 0 deletions test/typescript/t.external.ts
@@ -0,0 +1,13 @@
import i18next from 'i18next';

/**
* Use of the exported TFunction in external utility methods such as
* NamespaceConsumer children t
*/
function resolveText(t: i18next.TFunction, keyOrText?: string): undefined | string {
if (keyOrText && keyOrText.startsWith(':')) {
return t(keyOrText.substring(1, keyOrText.length));
} else {
return keyOrText;
}
}

0 comments on commit e0fa366

Please sign in to comment.