diff --git a/src/index.d.ts b/src/index.d.ts index 90affcc87..a8a6d9279 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -2,6 +2,7 @@ import i18next, { ReactOptions, i18n, ThirdPartyModule, WithT, TFunction, Resour import * as React from 'react'; type Omit = Pick>; +type Subtract = Omit; export type Namespace = string | string[]; @@ -92,9 +93,15 @@ export function withTranslation( options?: { withRef?: boolean; }, -):

( - component: React.ComponentType

, -) => React.ComponentType & WithTranslationProps>; +): < + C extends React.ComponentType & WithTranslationProps>, + ResolvedProps = JSX.LibraryManagedAttributes< + C, + Subtract, WithTranslationProps> + > +>( + component: C, +) => React.ComponentType & WithTranslationProps>; export interface I18nextProviderProps { i18n: i18n; diff --git a/src/ts4.1/index.d.ts b/src/ts4.1/index.d.ts index a9a5f9a4a..cbd3559a6 100644 --- a/src/ts4.1/index.d.ts +++ b/src/ts4.1/index.d.ts @@ -9,6 +9,8 @@ import i18next, { } from 'i18next'; import * as React from 'react'; +type Subtract = Omit; + /** * This interface can be augmented by users to add types to `react-i18next` default resources. */ @@ -176,9 +178,15 @@ export function withTranslation( options?: { withRef?: boolean; }, -):

>( - component: React.ComponentType

, -) => React.ComponentType> & WithTranslationProps>; +): < + C extends React.ComponentType & WithTranslationProps>, + ResolvedProps = JSX.LibraryManagedAttributes< + C, + Subtract, WithTranslationProps> + > +>( + component: C, +) => React.ComponentType> & WithTranslationProps>; export interface I18nextProviderProps { i18n: i18n; diff --git a/test/typescript/withTranslation.test.tsx b/test/typescript/withTranslation.test.tsx index f0af576d1..f2b7476a9 100644 --- a/test/typescript/withTranslation.test.tsx +++ b/test/typescript/withTranslation.test.tsx @@ -10,18 +10,29 @@ interface MyComponentProps extends WithTranslation { bar: 'baz'; } +const defaultProps: Partial = { + bar: 'baz', +}; + const MyComponent = (props: MyComponentProps) => { const { t, i18n, tReady } = props; const r: boolean = tReady; return

{t('title')}

; }; +MyComponent.defaultProps = defaultProps; + // page uses the hook function defaultUsage() { const ExtendedComponent = withTranslation()(MyComponent); return ; } +function defaultUsageWithDefaultProps() { + const ExtendedComponent = withTranslation()(MyComponent); + return ; +} + /** * @see https://react.i18next.com/latest/withtranslation-hoc#withtranslation-params */