Typescript - Tips & Tricks - Rest Parameters & Rest Arguments

Full Stack Developer and JavaScript and TypeScript enthusiastic.
Search for a command to run...

Full Stack Developer and JavaScript and TypeScript enthusiastic.
No comments yet. Be the first to comment.
In this series, You'll see some tips and tricks using Typescript
Today I want to start a series of tips and tricks about Typescript. I will try to publish two/three tips & tricks a week. Before start, I want to explain why I start this series. I start this series because every day I see too many any types in the t...
Today, I wanna show you how to set up your devices with Antigravity using only the agentic mode, without touching any code. At the last CodeMotion Rome, I got my special badge. A CM Node, a special ha

As a developer, you have to take control of your projects every day. Whether it is a company repository, an open source project you maintain or collaborate on, or a simple pet project. Gaining control over your projects often depends on the platform ...

My Antigravity first experience

Write clear and insightful commit messages using AI and GitLens.

We are in the AI era. New models emerge daily, and many applications have already integrated AI into their workflows. Gemini, OpenAI, Copilot, Deepseek, Llama, and many others enable AI in your applications or help you write code, but they all need a...

Hi guy and welcome back :) Today I'll talk about the Rest Parameters and the Rest Arguments. Sometimes we need to write a simple code like this:
function multiply(a: number, b: number): number {
return a * b;
}
const result1 = multiply(2, 2); // 4
const result2 = multiply(2, 3); // 3
but after a while the customer asks us to multiply for unbounded numbers, so we need to change our code. In these cases, typescript helps us with a feature called Rest Parameters. This feature permits us to identify this list of parameters as an array type so we can leave our consumer to call the method with the correct number of items he needs, and we can manipulate these parameters more easily. Therefore the previous example can be transformed in this way
function multiply(a: number, ...args: number[]): number {
return args.reduce((acc, curr) => acc * curr, a);
}
const result1 = multiply(2, 2, 2); // 8
const result2 = multiply(2, 3, 2, 2); // 24
As you can see, now the consumer can call the "multiply" method as he prefers and our code responds correctly in all cases. In this example, we have called the "multiply" method with a fixed number of elements, 3 in the first case and 4 in the second one. But in some cases, we don't know the correct number of elements and maybe they are in an array. To resolve these cases, typescript has another important feature tied to the Rest Parameters, the Rest Arguments. This feature helps us to convert the array elements to a list of parameters. Thus, we can call the "multiply" method in this way too.
const result1 = multiply(2, ...[2, 2]); // 8
const result2 = multiply(2, ...[3, 2, 2]); // 24
I think that these two features are really powerful, and can help you to manipulate large numbers of parameters and take your code simple and flexible.
And now a special greeting. This is the last post of this series Typescript - Tips & Tricks, I hope you enjoyed the series and it will help you in your daily work.
Many Thanks to have read the series. It's not a goodbye, I'll get back soon with other topics.
Bye, Bye Guys!