Skip to content
Related Articles

Related Articles

Rest Parameters in TypeScript

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 14 Feb, 2022

In Typescript, we can specify an indefinite number of arguments as an array using the rest parameter syntax. A function can be called with any number of arguments using the rest parameter, regardless of how it was defined. 

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 parameters should always come at last or the typescript compiler will raise errors.

Syntax: Below is the syntax of the rest parameter:

function function_name(...rest: type[]) {
  // Type of the is the type of the array. 
}

Example 1: Average of numbers using rest parameter. The below code is an example for the rest parameter where we pass in any number of arguments we want and the average of the numbers is displayed as output.

Javascript




function getAverage(...args: number[]) {
  var avg = args.reduce(function (a, b) {
      return a + b;
    }, 0) / args.length;
  
  return avg;
}
  
// Function call
console.log("Average of the given numbers is : "
    getAverage(10, 20, 30, 40, 50));
console.log("Average of the given numbers is : "
    getAverage(1.5, 3.5, 5.5, 7.5, 9.5));
console.log("Average of the given numbers is : "
    getAverage(2, 4, 6));


Output:

Average of the given numbers is : 30
Average of the given numbers is : 5.5
Average of the given numbers is : 4

Example 2: The rest parameter is of type string in this example. It takes in parameters of string type. There are two parameters in the below program. The rest parameter should always come at the last. We use the join() method to join the strings by a string ‘ , ‘. A simple string is returned in this example. 

Javascript




function Job(Job_title: string, 
    ...people: string[]) {
  return people.join(", ") + " are " + Job_title;
}
  
console.log(Job("mathematicians", "rachel"
    "john", "peter"));
console.log(Job("coders", "sarah", "joseph"));


Output:

rachel, john, peter are mathematicians
sarah, joseph are coders

Example 3: What happens if we define the rest parameter at the start? It takes only one parameter instead of taking an indefinite number of arguments. The typescript compiler will raise errors and the function will be of no use. 

Javascript




function Job(...people: string[], 
    Job_title: string) {
  return people.join(", ") + " are " + Job_title;
}
  
console.log(Job("rachel", "john", "peter"
    "Mathematicians"));
console.log(Job("sarah", "joseph", "coders"));


Output: Typescript compiler raised the error.

error TS1014: A rest parameter must be last in a parameter list.
76 function Job(...people: string[], Job_title: string) { ... }        

Reference: https://www.typescripttutorial.net/typescript-tutorial/typescript-rest-parameters/


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!