Append argument to a function in TypeScript

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

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

Parameters solution

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

Solution, version 1

Itโ€™s working just fine โ€“ ๐Ÿ”ฅ

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

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


Thatโ€™s it ๐Ÿ’ซ:

Thank you for your time ๐Ÿ™๐Ÿป


Let's chat and have fun ๐Ÿง‘โ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

No more often than once a week I write ๐Ÿ“„ about TypeScript ๐Ÿ’ช, video playback ๐Ÿ“บ and frontend related topics. You can unsubscribe in any time โ†˜๏ธ

ยฉ 2021, Built with Gatsby