Skip to content

Commit

Permalink
fix: 🐛 improve TypeScript typings of withT() HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 22, 2019
1 parent 59b977a commit f463493
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/createTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ export const createTranslations = (ns: string = 'main'): Result => {
return [state.createT ? state.createT(nss) : defaultT, state];
};

const withT: WithT = <T extends React.ComponentType>(Comp: T, nss: string | string[] = ns) => {
const withT: WithT = (Comp, nss = ns) => {
if (!Array.isArray(nss)) nss = [nss];
return (props => {
const [t, T] = useT(nss as string[]);
return React.createElement(Comp, {...(props as any), t, T});
}) as T;
});
};

const Trans: React.FC<TransProps> = (props) => {
Expand Down
9 changes: 7 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ export interface TransProps {
// React hook.
export type UseT = (namespaces?: string[]) => [TranslatorFn, ProviderState];

// Higler order component.
export type WithT = (Comp: React.SFC<any>, ns?: string | string[]) => React.SFC<any>;
export interface WithTProps {
t: TranslatorFn;
T: ProviderState;
}

// Higher order component.
export type WithT = <P extends WithTProps>(Comp: React.ComponentType<P>, ns?: string | string[]) => React.FC<Omit<P, 't' | 'T'>>;

export interface Result {
Provider: React.ComponentClass<ProviderProps, ProviderState>;
Expand Down

0 comments on commit f463493

Please sign in to comment.