Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
Interesting Facts About Functions in TypeScript
Next article icon

Explain about rest parameters and arguments in TypeScript

Last Updated : 22 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will try to understand all the basic facts or details which are associated with Rest Parameters and Arguments in TypeScript.

Rest Parameters:

  • Rest Parameter allows us to accept zero or more arguments of the specified type.
  • A function (or a method) has only one rest parameter.
  • This parameter appears at last in the function's specified parameter list.
  • Since here it is TypeScript being into the picture, so the type of rest parameter is of an Array type (further the array could be string data type or number data type or anything).

Syntax: Following is the rest parameter syntax provided in TypeScript.

function function_name (...rest_parameter_name : type[]) { }

Here these "..." (triple dots) represents the rest parameter syntax followed by name of the rest parameter followed by the type of that rest parameter (must be of Array data type).

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 itself.

Syntax: Following is the syntax of using the Rest Arguments in TypeScript (explained using an example).

let array_1 : number[] = [1 , 2 , 3]; let array_2 : number[] = [5 , 6, 7]; array_1.push(array_2);

The following examples will help us in order to understand the above-illustrated facts in a more clear manner (in case you don't know, these examples could only be runnable in your local PC if you have installed typescript in it, otherwise you may choose some online typescript compilers).

Example 1: In this example, we will get the sum of arguments of integers which are passed in a function using the rest parameter syntax.

JavaScript
function getSum(...numbers: number[]): number {     let sum = 0;     numbers.forEach((num) => sum += num);     return sum; }  // Function call console.log(getSum(10 , 50 , 30)); console.log(getSum(-50 , 50 , -10 , 5)); 

Output:

90 -5

Example 2: 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("Hello ", "GeeksforGeeks ", "ABCD ", "Apple")); 

Output:

Hello  GeeksforGeeks , ABCD , Apple!

Example 3: In this example, we will perform multiplication of all the numbers which are passed in a function as arguments.

JavaScript
let multiply = (a: number, ...numbers: number[]): number =>{   return numbers.reduce((acc, curr) => acc * curr, a); }  // Function call let result1 = multiply(2, 2, 2, 2, 7);  let result2 = multiply(2, 3, 2, 5, 8);   // Printing data console.log(result1); console.log(result2); 

Output:

112 480

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


Next Article
Interesting Facts About Functions in TypeScript
author
amansingla
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • TypeScript
  • Geeks-Premier-League-2022
  • JavaScript-Questions

Similar Reads

  • 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
    2 min read
  • Explain the Rest parameter in ES6
    In this article, we will try to understand the details associated with the rest parameter in ES6 which includes the syntax of rest parameters, and how to use this with the help of certain examples. The rest parameter is an improved way to handle function parameters, allowing us to handle various inp
    3 min read
  • Interesting Facts About Generics in TypeScript
    TypeScript generics provide a powerful way to create reusable, type-safe components and functions that work with multiple types. They improve flexibility, reduce redundancy, and ensure strict type constraints without sacrificing reusability. 1. Generics Make Functions FlexibleGenerics allow you to c
    3 min read
  • Interesting Facts About Functions in TypeScript
    TypeScript enhances JavaScript functions with strong typing, better inference, and advanced features, making code safer, more readable, and more scalable. Understanding its function-related capabilities can greatly improve development efficiency. 1. TypeScript Infers Function Argument TypesTypeScrip
    3 min read
  • Rest Parameters in TypeScript
    Rest parameters in TypeScript enable functions to handle an unlimited number of arguments by grouping them into an array. They are defined using ... and must be the last parameter. Allow flexible and dynamic input handling in functions.Simplify working with multiple arguments without specifying them
    3 min read
  • TypeScript Using Type Parameters in Generic Constraints
    TypeScript Generics allows you to write reusable code that works with different types. In this article, we will explore how to use type parameters in generic constraints to ensure that which types are allowed in generic types or functions. Syntax:function functionName<Type extends ConstraintType
    2 min read
  • TypeScript Parameter Type Annotations
    TypeScript Parameter type annotations are used to specify the expected data types of function parameters. They provide a way to explicitly define the types of values that a function expects as arguments. Parameter type annotations are part of TypeScript's static typing system, and they help catch ty
    2 min read
  • Interesting Facts About Object Types and Interfaces in TypeScript
    TypeScript enhances object types and interfaces with strong typing, extendibility, and dynamic features, making code more structured and maintainable. Mastering these concepts improves scalability, flexibility, and type safety in applications. 1. Interfaces Can Describe FunctionsInterfaces in TypeSc
    3 min read
  • TypeScript Optional Parameters in Callbacks
    In TypeScript, optional parameters in callbacks are parameters that may or may not be provided when the callback function is invoked. They are denoted by a ? after the parameter name, allowing for more flexible function calls without requiring all arguments. Syntaxtype MyCallback = (param1: string,
    2 min read
  • TypeScript Generic Parameter Defaults
    TypeScript Generic Parameter Defaults allow you to specify default values for generic type parameters. This feature provides a fallback type when no explicit type argument is given, making generic types more flexible and easier to use, particularly when type inference is not straightforward. Syntaxf
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences