Skip to content

Commit fee5755

Browse files
author
João Moura
committed
Fixed typings for HOC props
1 parent 651a9e5 commit fee5755

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
"react": "^16.1.0"
3333
},
3434
"devDependencies": {
35+
"@types/hoist-non-react-statics": "^3.3.1",
3536
"@types/react": "^16.8.20",
3637
"react": "^16.8.6",
3738
"rimraf": "^2.6.3",
3839
"tslint": "^5.17.0",
39-
"typescript": "^3.5.2"
40+
"typescript": "^3.5.2",
41+
"utility-types": "^3.7.0"
4042
},
4143
"dependencies": {
4244
"hoist-non-react-statics": "^2.3.1"

src/index.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import React from 'react'
2-
3-
// statics handler
41
import hoistStatics from 'hoist-non-react-statics'
2+
import React from 'react'
3+
import { Subtract } from 'utility-types'
54

65
let language: string = 'pt'
76
let id: number = 1
@@ -20,7 +19,9 @@ export interface ITranslations {
2019
[key: string]: ITranslation
2120
}
2221

23-
export type T = (path: string, args?: {[key: string]: string}) => string
22+
export interface ITranslate {
23+
t(path: string, args?: {[key: string]: string}): string
24+
}
2425

2526
let translations: ITranslations = {}
2627

@@ -106,8 +107,10 @@ export function t(path: string, args?: {[key: string]: string}): string {
106107
return translation
107108
}
108109

109-
export function translate(Component: React.ComponentType<any>): React.ComponentType<any> {
110-
class TranslatedComponet extends React.Component<any, any> {
110+
export function translate<P extends ITranslate>(
111+
Component: React.ComponentType<P>
112+
): React.ComponentType<Subtract<P, ITranslate>> {
113+
class TranslatedComponent extends React.Component<Subtract<P, ITranslate>> {
111114
public id: number | undefined
112115

113116
public componentDidMount() {
@@ -121,11 +124,11 @@ export function translate(Component: React.ComponentType<any>): React.ComponentT
121124
}
122125

123126
public render() {
124-
return <Component {...this.props} t={t}/>
127+
return <Component {...this.props as P} t={t}/>
125128
}
126129
}
127130

128-
return hoistStatics(TranslatedComponet, Component)
131+
return hoistStatics(TranslatedComponent, Component)
129132
}
130133

131134
export default {

0 commit comments

Comments
 (0)