Skip to content

Commit 09bfa0c

Browse files
authored
Merge pull request #1 from tkow/feature/add-traslate-generic
Rewrite interface with WithT interface introduced
2 parents 77f66e2 + bb28274 commit 09bfa0c

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"eslint-plugin-jsx-a11y": "6.1.1",
5858
"eslint-plugin-prettier": "2.7.0",
5959
"eslint-plugin-react": "7.11.1",
60-
"i18next": "13.1.0",
60+
"i18next": "^13.1.4",
6161
"jest": "23.6.0",
6262
"jest-cli": "23.6.0",
6363
"mkdirp": "0.5.1",
@@ -78,7 +78,7 @@
7878
"yargs": "12.0.2"
7979
},
8080
"peerDependencies": {
81-
"i18next": ">= 6.0.1",
81+
"i18next": ">= 13.1.3",
8282
"react": ">= 16.3.0"
8383
},
8484
"scripts": {

src/index.d.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ interface ReactI18nextModule {
1414
init: (instance: i18next.i18n) => void;
1515
}
1616

17-
interface ReactI18nextTranslateFunction {
18-
t<TKeys extends string=string,TVals extends object =object,TResult=any>
19-
(key: TKeys | TKeys[], options?: i18next.TranslationOptions<TVals>):TResult;
20-
}
21-
2217
export const reactI18nextModule: ReactI18nextModule;
2318

2419
export function setDefaults(options: ReactI18NextOptions): void;
@@ -29,7 +24,7 @@ export function setI18n(instance: i18next.i18n): void;
2924

3025
export function getI18n(): i18next.i18n;
3126

32-
export interface I18nContextValues extends ReactI18nextTranslateFunction{
27+
export interface I18nContextValues extends i18next.WithT {
3328
i18n: i18next.i18n;
3429
defaultNS?: string;
3530
reportNS?: string;
@@ -80,7 +75,7 @@ export interface NamespacesConsumerProps extends ReactI18NextOptions {
8075
initialI18nStore?: {};
8176
initialLanguage?: string;
8277
children: (
83-
t: ReactI18nextTranslateFunction['t'],
78+
t: i18next.WithT['t'],
8479
options: {
8580
i18n: i18next.i18n;
8681
lng: string;
@@ -100,7 +95,7 @@ export interface I18nextProviderProps {
10095

10196
export const I18nextProvider: React.ComponentClass<I18nextProviderProps>;
10297

103-
export interface TransProps extends Partial<ReactI18nextTranslateFunction>{
98+
export interface TransProps extends Partial<i18next.WithT>{
10499
i18nKey?: string;
105100
count?: number;
106101
parent?: React.ReactNode;

test/typescript/GenericTlanslateFunction.test.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import {
88

99
type TKeys = "title" | "text";
1010

11-
declare interface IWithNamespacesOverrideTest extends WithNamespaces {
12-
t<T extends string| string[]>(a: T): any;
13-
}
14-
1511
function NamespacesConsumerTest() {
1612
return (
1713
<NamespacesConsumer>
@@ -22,6 +18,7 @@ function NamespacesConsumerTest() {
2218
<span>{t("any", {anyObject: {}})}</span>
2319
<span>{t<TKeys>("text")}</span>
2420
<span>{t<TKeys, {key: string}>("text", {key: "foo"})}</span>
21+
<span>{t<TKeys, {key: "bar"}, string>("text", {key: "bar"})}</span>
2522
</div>
2623
}
2724
</NamespacesConsumer>
@@ -45,7 +42,16 @@ const MyComponentWrapped = withNamespaces()(TransComponentTest);
4542

4643
type ArticleKeys = "article.part1" | "article.part2";
4744
type AnotherArticleKeys = "anotherArticle.part1" | "anotherArticle.part2";
48-
class App extends React.Component<WithNamespaces> {
45+
46+
/**
47+
* Overload makes completion of arguments by without specifying type parameters
48+
*/
49+
interface IOverloadedWithNamespaces extends WithNamespaces {
50+
t(key: ArticleKeys, b?: object): any;
51+
t<T extends AnotherArticleKeys>(key: T, b: {name: string}): any;
52+
}
53+
54+
class App extends React.Component<IOverloadedWithNamespaces> {
4955
public render() {
5056
const { t, i18n } = this.props;
5157

@@ -64,12 +70,12 @@ class App extends React.Component<WithNamespaces> {
6470
<MyComponentWrapped />
6571
</div>
6672
<article>
67-
<div>{t<ArticleKeys>("article.part1")}</div>
68-
<div>{t<ArticleKeys>("article.part2")}</div>
73+
<div>{t("article.part1", {name: "foo"})}</div>
74+
<div>{t("article.part2")}</div>
6975
</article>
7076
<article>
71-
<div>{t<AnotherArticleKeys>("anotherArticle.part1")}</div>
72-
<div>{t<AnotherArticleKeys>("anotherArticle.part2")}</div>
77+
<div>{t<AnotherArticleKeys>("anotherArticle.part1", {name: "foo"})}</div>
78+
<div>{t<AnotherArticleKeys>("anotherArticle.part2", {name: "bar"})}</div>
7379
</article>
7480
</div>
7581
);

0 commit comments

Comments
 (0)