3

Example:

function a(...args: ???type of b() params???) {
  b(...args)
}

I want args to be type of b's parameters.

If you want to know why would I want that, then it is for code readability/incapsulation. b is imported function, and I don't care about its implementation at the level of function a declaration

2 Answers 2

5

You can do this in 3.0 Tuples in rest parameters and spread expressions (in RC right now should be out soon)

type ArgumentType<T> = T extends (...args: infer U) => any ? U : never;

function a(...args: ArgumentType<typeof b>) {
    function b(...args: any[]) {

    }
}
// More complex examples
function a2(...args: ArgumentType<typeof b>) {
    function b(name: string, value: number) {

    }
}

a2("", 1);
Sign up to request clarification or add additional context in comments.

Comments

1

This is possible another way now:

function a(...args: Parameters<typeof b>) {
  b(...args);
}

playground link

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.