Skip to content

Commit f463493

Browse files
committed
fix: 🐛 improve TypeScript typings of withT() HOC
1 parent 59b977a commit f463493

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/createTranslations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ export const createTranslations = (ns: string = 'main'): Result => {
142142
return [state.createT ? state.createT(nss) : defaultT, state];
143143
};
144144

145-
const withT: WithT = <T extends React.ComponentType>(Comp: T, nss: string | string[] = ns) => {
145+
const withT: WithT = (Comp, nss = ns) => {
146146
if (!Array.isArray(nss)) nss = [nss];
147147
return (props => {
148148
const [t, T] = useT(nss as string[]);
149149
return React.createElement(Comp, {...(props as any), t, T});
150-
}) as T;
150+
});
151151
};
152152

153153
const Trans: React.FC<TransProps> = (props) => {

src/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ export interface TransProps {
4949
// React hook.
5050
export type UseT = (namespaces?: string[]) => [TranslatorFn, ProviderState];
5151

52-
// Higler order component.
53-
export type WithT = (Comp: React.SFC<any>, ns?: string | string[]) => React.SFC<any>;
52+
export interface WithTProps {
53+
t: TranslatorFn;
54+
T: ProviderState;
55+
}
56+
57+
// Higher order component.
58+
export type WithT = <P extends WithTProps>(Comp: React.ComponentType<P>, ns?: string | string[]) => React.FC<Omit<P, 't' | 'T'>>;
5459

5560
export interface Result {
5661
Provider: React.ComponentClass<ProviderProps, ProviderState>;

0 commit comments

Comments
 (0)