1

I want to get the parameters of a function that is saved in different modules. For example myFunction gets the function I desired, then it should be executed with the correct parameters. However the Parameters line shows the following error: Type 'Function' does not satisfy the constraint

public Execute(module: any, myFunction: Function, params: string){
     type TestArgsType = Parameters<typeof myFunction>;
     myFunction.apply(module, params);
}

Is there any way to use the Parameters with a function passed as a parameter?

1 Answer 1

1

You can if you make yours generic.

const Execute = <T extends (...args: unknown[]) => unknown>(module: any, myFunction: T, params: string){
     type TestArgsType = Parameters<T>;

or

const Execute = <TestArgsType extends unknown[]>(module: any, myFunction: (...args: T) => unknown, params: string){

You might also consider typing module to be something more precise, or at least unknown - using any defeats the purpose of TypeScript.

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.