Rest Parameters in TypeScript Last Updated : 22 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 individually.Syntaxfunction function_name(...rest: type[]) { // Type of the is the type of the array. }Parameters:functionName: The name of your function....rest: The rest parameter that collects all additional arguments into an array.type[]: Specifies the type of elements in the rest array (e.g., number[], string[]). JavaScript function sum(...numbers: number[]): number { return numbers.reduce((total, num) => total + num, 0); } console.log(sum(1, 2, 3)); console.log(sum(10, 20)); The sum function uses the rest parameter ...numbers to collect all arguments passed to it into an array of type number[].The reduce method is applied to the numbers array, adding all its elements together to compute the total.Output:630Calculating the Average of Numbers TypeScript function average(...numbers: number[]): number { let total = 0; for (let num of numbers) { total += num; } return numbers.length === 0 ? 0 : total / numbers.length; } console.log("Average of the given numbers is:", average(10, 20, 30, 60)); console.log("Average of the given numbers is:", average(5, 6)); console.log("Average of the given numbers is:", average(4)); The average function uses a rest parameter ...numbers to accept any number of numeric arguments.It calculates the total sum of these numbers and returns their average.Output:Average of the given numbers is : 30Average of the given numbers is : 5.5Average of the given numbers is : 4Concatenating Strings TypeScript function joinStrings(...strings: string[]): string { return strings.join(', '); } console.log(joinStrings("rachel", "john", "peter") + " are mathematicians"); console.log(joinStrings("sarah", "joseph") + " are coders"); The joinStrings function accepts multiple string arguments using a rest parameter.It concatenates them into a single string, separated by commas.Output:rachel, john, peter are mathematicianssarah, joseph are codersIncorrect Usage of Rest Parameters TypeScript // Incorrect usage - will raise a compiler error function job(...people: string[], jobTitle: string): void { console.log(`${people.join(', ')} are ${jobTitle}`); } // Uncommenting the below line will cause a compiler error // job("rachel", "john", "peter", "mathematicians"); In this example, the rest parameter ...people is not placed at the end of the parameter list.TypeScript requires rest parameters to be the last parameter; otherwise, a compiler error occurs.Output: Typescript compiler raised the error.main.ts(2,14): error TS1014: A rest parameter must be last in a parameter list. Best Practices for Using TypeScript Rest ParametersPlace Rest Parameters Last: Always define rest parameters at the end of the parameter list to ensure correct function behavior. Use Appropriate Types: Specify the correct array type for rest parameters to maintain type safety and code clarity. Limit to One Rest Parameter: A function should have only one rest parameter to avoid complexity and potential errors. Avoid Overuse: Use rest parameters judiciously; overuse can lead to code that is hard to understand and maintain. Comment More infoAdvertise with us Next Article Rest Parameters in TypeScript S sarahjane3102 Follow Improve Article Tags : TypeScript JavaScript-Questions Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Like