Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withTranslation() typing fix for defaultProps #1226

Merged
merged 3 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import i18next, { ReactOptions, i18n, ThirdPartyModule, WithT, TFunction, Resour
import * as React from 'react';

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type Subtract<T extends K, K> = Omit<T, keyof K>;

export type Namespace = string | string[];

Expand Down Expand Up @@ -92,9 +93,15 @@ export function withTranslation(
options?: {
withRef?: boolean;
},
): <P extends WithTranslation>(
component: React.ComponentType<P>,
) => React.ComponentType<Omit<P, keyof WithTranslation> & WithTranslationProps>;
): <
C extends React.ComponentType<React.ComponentProps<C> & WithTranslationProps>,
ResolvedProps = JSX.LibraryManagedAttributes<
C,
Subtract<React.ComponentProps<C>, WithTranslationProps>
>
>(
component: C,
) => React.ComponentType<Omit<ResolvedProps, keyof WithTranslation> & WithTranslationProps>;

export interface I18nextProviderProps {
i18n: i18n;
Expand Down
14 changes: 11 additions & 3 deletions src/ts4.1/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import i18next, {
} from 'i18next';
import * as React from 'react';

type Subtract<T extends K, K> = Omit<T, keyof K>;

/**
* This interface can be augmented by users to add types to `react-i18next` default resources.
*/
Expand Down Expand Up @@ -176,9 +178,15 @@ export function withTranslation<N extends Namespace = DefaultNamespace>(
options?: {
withRef?: boolean;
},
): <P extends WithTranslation<N>>(
component: React.ComponentType<P>,
) => React.ComponentType<Omit<P, keyof WithTranslation<N>> & WithTranslationProps>;
): <
C extends React.ComponentType<React.ComponentProps<C> & WithTranslationProps>,
ResolvedProps = JSX.LibraryManagedAttributes<
C,
Subtract<React.ComponentProps<C>, WithTranslationProps>
>
>(
component: C,
) => React.ComponentType<Omit<ResolvedProps, keyof WithTranslation<N>> & WithTranslationProps>;

export interface I18nextProviderProps {
i18n: i18n;
Expand Down
11 changes: 11 additions & 0 deletions test/typescript/withTranslation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,29 @@ interface MyComponentProps extends WithTranslation {
bar: 'baz';
}

const defaultProps: Partial<MyComponentProps> = {
bar: 'baz',
};

const MyComponent = (props: MyComponentProps) => {
const { t, i18n, tReady } = props;
const r: boolean = tReady;
return <h2>{t('title')}</h2>;
};

MyComponent.defaultProps = defaultProps;

// page uses the hook
function defaultUsage() {
const ExtendedComponent = withTranslation()(MyComponent);
return <ExtendedComponent bar="baz" />;
}

function defaultUsageWithDefaultProps() {
const ExtendedComponent = withTranslation()(MyComponent);
return <ExtendedComponent />;
}

/**
* @see https://react.i18next.com/latest/withtranslation-hoc#withtranslation-params
*/
Expand Down