I'm creating a higher-order component to wrap a component extending the interface:
interface ClickOutsideInterface {
onClickOutside: (e: Event) => void
}
The factory I've created expects a React.ComponentClass implementing the ClickOutsideInterface:
function clickOutside<P>(Component: React.ComponentClass<P> & ClickOutsideInterface){
return class ClickOutside extends React.Component<P, {}> {
/* on click outside invoke Component.onClickOutside */
render() {
return(<div><Component {...this.props} /></div>)
}
}
}
To test the factory, I've implement a Component extending the ClickOutsideInterface
class Test extends React.Component<TProps, TState> implements ClickOutsideInterface {
onClickOutside(event: Event): void {
this.setState({
closed: true
})
}
render(){
return(
<div>{this.state.closed}</div>
)
}
}
But when I use the component as an argument in the function clickOutside:
const withClickOutside = clickOutside(Test)
I get the following type error for argument Test:
Argument of type 'typeof Test' is not assignable to parameter of type 'ComponentClass & ClickOutsideInterface'. Type 'typeof Test' is not assignable to type 'ClickOutsideInterface'. Property 'onClickOutside' is missing in type 'typeof Test'.
Any ideas why Typescript believes I'm not implementing the interface in Test?
withClickOutside()(Test)clickOutsidereturned a function which could be used as a decorator. That wouldn't make sense here anyway because it wouldn't take any arguments.