From 9440fd14094e78c2db7345583a3aba9b36f0eefb Mon Sep 17 00:00:00 2001 From: Netanel Gilad Date: Tue, 28 Aug 2018 13:37:33 +0300 Subject: [PATCH 1/2] add HOC props inference to v4 of react-i18next --- types/react-i18next/v4/index.d.ts | 3 ++- types/react-i18next/v4/translate.d.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/types/react-i18next/v4/index.d.ts b/types/react-i18next/v4/index.d.ts index 14676aba7e..758a00edef 100644 --- a/types/react-i18next/v4/index.d.ts +++ b/types/react-i18next/v4/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for react-i18next 4.6 // Project: https://github.com/i18next/react-i18next // Definitions by: Giedrius Grabauskas +// Netanel Gilad // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -31,7 +32,7 @@ export { * interface MyComponentProps extends ReactI18next.InjectedTranslateProps {} */ export interface InjectedTranslateProps { - t?: TranslationFunction; + t: TranslationFunction; } export as namespace reactI18Next; diff --git a/types/react-i18next/v4/translate.d.ts b/types/react-i18next/v4/translate.d.ts index d41b4dc00a..ff715036c4 100644 --- a/types/react-i18next/v4/translate.d.ts +++ b/types/react-i18next/v4/translate.d.ts @@ -1,5 +1,6 @@ import * as React from "react"; import { i18n } from "i18next"; +import { InjectedTranslateProps } from "react-i18next"; export interface TranslateOptions { withRef?: boolean; @@ -11,5 +12,14 @@ export interface TranslateOptions { i18n?: i18n; } +// Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766 +type Omit = Pick; + +// Injects props and removes them from the prop requirements. +// Adds the new properties t (or whatever the translation function is called) and i18n if needed. +export type InferableComponentEnhancerWithProps = +

(component: React.ComponentClass

| React.StatelessComponent

) => + React.ComponentClass>; + // tslint:disable-next-line:ban-types -export default function translate(namespaces?: TKey[] | TKey, options?: TranslateOptions): (WrappedComponent: C) => C; +export default function translate(namespaces?: TKey[] | TKey, options?: TranslateOptions): InferableComponentEnhancerWithProps<"t">; From 8de0e983663143df793f01cc80b6330946059fef Mon Sep 17 00:00:00 2001 From: Netanel Gilad Date: Tue, 28 Aug 2018 14:18:56 +0300 Subject: [PATCH 2/2] fix react-i18next-tests --- types/react-i18next/v4/react-i18next-tests.tsx | 15 +++++++++------ types/react-i18next/v4/translate.d.ts | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/types/react-i18next/v4/react-i18next-tests.tsx b/types/react-i18next/v4/react-i18next-tests.tsx index 6105ccf921..3e57dd091b 100644 --- a/types/react-i18next/v4/react-i18next-tests.tsx +++ b/types/react-i18next/v4/react-i18next-tests.tsx @@ -17,17 +17,16 @@ const AnotherComponent = translate('view', { wait: true, translateFuncName: '_' class InnerYetAnotherComponent extends React.Component { render() { - const t = this.props.t!; + const t = this.props.t; return

{t('usingDefaultNS', { /* options t options */ })}

; } } const YetAnotherComponent = translate()(InnerYetAnotherComponent); -@translate(['view', 'nav'], { wait: true }) class TranslatableView extends React.Component { render() { - const t = this.props.t!; + const t = this.props.t; const interpolateComponent = "a interpolated component"; return ( @@ -55,12 +54,14 @@ class TranslatableView extends React.Component { } } +const WrappedTranslatableView = translate(['view', 'nav'], { wait: true })(TranslatableView); + class App extends React.Component { render() { return (
- +
); @@ -81,12 +82,14 @@ loadNamespaces({ components: [App], i18n }).then(() => { }).catch(error => { }); type Key = "view" | "nav"; -@translate(['view', 'nav']) class GenericsTest extends React.Component { render() { return null; } } -@translate('view') +translate(['view', 'nav'])(GenericsTest); + class GenericsTest2 extends React.Component { render() { return null; } } + +translate('view')(GenericsTest2); diff --git a/types/react-i18next/v4/translate.d.ts b/types/react-i18next/v4/translate.d.ts index ff715036c4..9a4fb6ddc2 100644 --- a/types/react-i18next/v4/translate.d.ts +++ b/types/react-i18next/v4/translate.d.ts @@ -13,7 +13,7 @@ export interface TranslateOptions { } // Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766 -type Omit = Pick; +export type Omit = Pick; // Injects props and removes them from the prop requirements. // Adds the new properties t (or whatever the translation function is called) and i18n if needed.