beraliv

Append argument type into function type in TypeScript

Example of AppendArgument use
Example of AppendArgument use

Today we discuss AppendArgument

I didn't have that example in production so not sure if it's practical at all.

Anyway let's try to solve it 🚀

Infer arguments of a function

We already solved ReturnType challenge:

ReturnType solution
ReturnType solution

Similarly we can define Parameters (which exists out of the box in TypeScript):

Parameters solution
Parameters solution

Let's use them both to infer Parameters and ReturnType for Fn and append another argument:

Solution, version 1
Solution, version 1

It's working just fine – https://tsplay.dev/WGnZvm 🔥

Saving the names of function arguments in TypeScript

But what I want to improve here is to have the same labels (names of arguments in the function signature) we had before. If we hover over any example, we will see the difference:

Problem of solution, version 1
Problem of solution, version 1

We can achieve it with adding [...args: Args] instead [...Args]:

Solution
Solution

That's it 💫: https://tsplay.dev/wgrb4W

Thank you for your time 🙏🏻

typescript

Comments

Alexey Berezin profile image

Written by Alexey Berezin who loves London 🏴󠁧󠁢󠁥󠁮󠁧󠁿, players ⏯ and TypeScript 🦺 Follow me on Twitter