Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6290

React generic type prop must extend other type?

$
0
0

I'm kinda new to typescript so this one is confusing for me. I wanted to pass generic type into my hoc. I need to pass component props as a generic type to get Component with that types on it.

const withHOC = <T extends {}>(Component: ComponentType<T>) => (props: T) => {   return (<Component {...props} />   )}//example: const MyView = withHoc<ViewProps>(View)

What I don't understand is that I can't just set <T> as generic type, I must set it as <T extends something>. With that said if I don't pass generic type (as shown in example) I wont get typescript warning for not passing generic type.Can someone explain me why is that happening?

I want it to look like this:

const withHOC = <T>(Component: ComponentType<T>) => (props: T) => {   return (<Component {...props} />   )}

So when I don't pass generic type when calling withHOC, it warns me there has to be one. I might me doing everything wrong and asking for something that is not achievable so correct me if I'm wrong.


Viewing all articles
Browse latest Browse all 6290

Trending Articles