Reputation: 7212
This is a simplified example of passing to a generic function an object and the name of a method in that object.
function invoke<TYPE_OBJ extends object>(obj: TYPE_OBJ, methodName: keyof TYPE_OBJ){
return obj[methodName](); //*** This expression is not callable. Type '{}' has no call signatures.ts(2349)
}
What am I missing to specify that the methodName corresponds to a callable thing?
ts version 4.2.4
Upvotes: 0
Views: 72
Reputation: 7304
I don't know exactly how is you case, however this seems working fine:
//example type
type TYPE_OBJ = {
foo: () => void;
};
function invoke<TYPE_OBJ extends Record<string, () => void>>(obj: TYPE_OBJ, methodName: keyof TYPE_OBJ) {
return obj[methodName]();
}
Upvotes: 2