In React, I have a generic interface like so:
interface BaseProps<T> {
data: T;
}
The consumers of my code will extend this to define their own Props and Component:
interface MyProps extends BaseProps<string> {
...
}
const MyComponent: React.FC<MyProps> = ({ data }) => {
...
}
I want to write a method that accepts such a component and returns a value of the same type as MyComponent.data. This is what I'm struggling with. I'm trying to write this method and invoke it as:
function myFunc<T, U extends BaseProps<T>>(component: React.FC<U>): T {
return ...
}
const ret1 = myFunc(MyComponent);
const ret2 = myFunc<string, MyProps>(MyComponent);
ret2 has the correct type string but ret comes out to have unknown type. Is there a way I can enforce constraints in the myFunc method such that ret1 is typed correctly without requiring me to explicitly declare types in <>?
Update
Both @futut and @proton have given (almost) identical solution that clearly work in this case! Kudos!
However, the sample code I wrote here was a bit over-simplified. If I change the BaseProps interface to:
interface BaseProps<T> {
callback: (value: T) => void;
}
Then I can no longer use U["data"]. Is there a way to be able to return a value of the type of value parameter in MyComponent.callback in this case?
PS: Apologies for shifting the goalpost here. My bad at oversimplifying too much.