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

Improve typings for HOC's and fix imports #567

Merged
merged 5 commits into from Oct 25, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions index.d.ts
@@ -1,19 +1,19 @@
import * as React from 'react';
import i18next, {
import {
i18n,
TranslationFunction,
ReactOptions as I18nOptions,
} from 'i18next';
import createReactContext, {
Context as ReactContext,
} from 'create-react-context';
import { Context as ReactContext } from 'create-react-context';

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

export interface ReactI18NextOptions extends I18nOptions {
usePureComponent?: boolean;
omitBoundRerender?: boolean;
}

type i18n = i18next.i18n;

interface ReactI18nextModule {
type: string;
init: (instance: i18n) => void;
Expand Down Expand Up @@ -44,8 +44,8 @@ export interface WithI18n extends I18nContextValues {
}

export function withI18n(): <P extends object>(
Wrapper: React.ComponentType<P & WithI18n>,
) => React.ComponentType<P>;
Wrapper: React.ComponentType<P>,
) => React.ComponentType<Subtract<P, WithI18n>>;

export interface WithNamespaces extends WithI18n {
tReady: boolean;
Expand All @@ -61,16 +61,22 @@ export interface WithNamespacesOptions extends ReactI18NextOptions {
| React.RefObject<HTMLElement | SVGElement | React.Component>;
}

type Namespace = string | string[];
interface NamespaceExtractor {
(props: any & { namespace: Namespace }): Namespace;
}

export function withNamespaces(
namespace?: string,
namespace?: Namespace | NamespaceExtractor,
options?: WithNamespacesOptions,
): <P extends object>(
component: React.ComponentType<P & WithNamespaces>,
) => React.ComponentType<P>;
): <P extends WithNamespaces>(
component: React.ComponentType<P>,
) => React.ComponentType<Subtract<P, WithNamespaces>>;

export const translate: typeof withNamespaces;

export interface NamespacesConsumerProps extends ReactI18NextOptions {
ns: Namespace;
initialI18nStore?: {};
initialLanguage?: string;
children(
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -41,6 +41,7 @@
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"@types/i18next": "^11.9.0",
"@types/react": "^16.4.18",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "23.6.0",
Expand Down