0

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

1 Answer 1

2

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]();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.