Rest Arguments

  • These are arguments that we pass in while merging two data types (suppose concatenation of two arrays) using the same “…” (triple dots).
  • These types of arguments are generally used for merging or concatenation or for any other operational purpose by itself.

Syntax

let generateGreeting = (greeting: string, ...names: string[]): string => {
    return greeting + " " + names.join(", ") + "!";
}

Example: In this example, we will concatenate several strings which are passed inside the function as arguments.

Javascript




let generateGreeting = 
    (greeting: string, ...names: string[]): string => {
    return greeting + " " + names.join(", ") + "!";
}
  
// Function call
console.log(generateGreeting("w3wiki ",
                             "is a computer science portal "));


Output:

w3wiki  is a computer science portal !

Reference: https://www.typescriptlang.org/docs/handbook/2/functions.html#rest-parameters-and-arguments



TypeScript Rest Parameters and Arguments

TypeScript Rest Parameters allow functions to accept an indefinite number of arguments of the same type, collecting them into an array. Arguments refer to the actual values passed to a function when it’s invoked, while Rest Parameters provide a way to handle multiple arguments as an array within the function.

Similar Reads

Rest Parameters

A rest parameter allows us to pass zero or any number of arguments of the specified type to a function. In the function definition where we specify function parameters rest of the parameters should always come at last or the typescript compiler will raise errors....

Rest Arguments

...