📖 Scenario: You are building a simple calculator that can add any number of numbers together.Users will give you many numbers, and you want to add them all up.
🎯 Goal: Create a function that uses rest parameters with types to accept any number of numbers and returns their sum.
📋 What You'll Learn
Create a function called sumNumbers that uses rest parameters with type number[].
Inside the function, add all the numbers together.
Call the function with multiple numbers.
Print the result.
💡 Why This Matters
🌍 Real World
Functions with rest parameters are useful when you don't know how many inputs you will get, like adding many numbers or combining many strings.
💼 Career
Understanding rest parameters and types is important for writing flexible and type-safe functions in TypeScript, a skill valuable for many programming jobs.
Progress0 / 4 steps
1
Create the function with rest parameters
Write a function called sumNumbers that takes rest parameters named numbers with type number[].
Typescript
Hint
Use ...numbers: number[] to accept many numbers as an array.
2
Add the numbers inside the function
Inside the sumNumbers function, create a variable called total set to 0. Then use a for loop with variable num to add each number in numbers to total.
Typescript
Hint
Use a for...of loop to add each number to total.
3
Call the function with multiple numbers
Call the sumNumbers function with the numbers 5, 10, and 15, and store the result in a variable called result.
Typescript
Hint
Call the function with the numbers separated by commas inside the parentheses.
4
Print the result
Write a console.log statement to print the value of the variable result.
Typescript
Hint
Use console.log(result) to show the sum.
Practice
(1/5)
1. What does the rest parameter syntax ...args: number[] do in a TypeScript function?
easy
A. It collects all remaining arguments into an array of numbers.
B. It restricts the function to accept only one number argument.
C. It declares a single number variable named args.
D. It spreads an array of numbers into separate arguments.
Solution
Step 1: Understand rest parameter syntax
The ...args gathers all extra arguments passed to the function into one array.
Step 2: Recognize the type annotation
The : number[] means this array contains only numbers.
Final Answer:
It collects all remaining arguments into an array of numbers. -> Option A
Quick Check:
Rest parameter with type = collects arguments as typed array [OK]
Hint: Rest parameters gather extra arguments into a typed array [OK]
Common Mistakes:
Confusing rest parameter with spread operator
Thinking it accepts only one argument
Ignoring the type annotation on the rest parameter
2. Which of the following is the correct syntax to declare a function with a rest parameter of strings in TypeScript?
easy
A. function greet(...names: string[]) {}
B. function greet(names: ...string[]) {}
C. function greet(...names: string) {}
D. function greet(...names: [string]) {}
Solution
Step 1: Identify correct rest parameter syntax
The rest parameter must use ... before the parameter name and be typed as an array type.
Step 2: Check each option
function greet(...names: string[]) {} uses ...names: string[], which is correct. Others misuse the spread or array syntax.
Final Answer:
function greet(...names: string[]) {} -> Option A
Quick Check:
Rest parameter syntax = ...name: type[] [OK]
Hint: Rest parameter needs ... before name and array type after [OK]
Common Mistakes:
Placing ... after the parameter name
Using a single type instead of an array type
Using tuple syntax instead of array type
3. What is the output of this TypeScript code?
function sum(...nums: number[]) {
return nums.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4));
The ...nums collects all arguments into an array: [1, 2, 3, 4].
Step 2: Calculate sum using reduce
The reduce adds all numbers: 1+2=3, 3+3=6, 6+4=10.
Final Answer:
10 -> Option D
Quick Check:
Sum of 1,2,3,4 = 10 [OK]
Hint: Rest gathers numbers; reduce sums them [OK]
Common Mistakes:
Concatenating numbers as strings
Forgetting initial value in reduce
Misunderstanding rest parameter as single number
4. Identify the error in this TypeScript function:
function joinStrings(...strings: string) {
return strings.join(", ");
}
medium
A. The function should return an array, not a string.
B. The join method cannot be used on strings.
C. Rest parameter must be typed as an array, e.g., string[].
D. Rest parameter cannot be used with strings.
Solution
Step 1: Check rest parameter type
The rest parameter must be typed as an array type, like string[], not just string.
Step 2: Understand why join fails
Since strings is typed as a single string, join method is invalid.
Final Answer:
Rest parameter must be typed as an array, e.g., string[]. -> Option C
Quick Check:
Rest parameter type = array type [OK]
Hint: Rest parameter type must be an array type [OK]
Common Mistakes:
Typing rest parameter as a single element type
Trying to call array methods on a non-array
Ignoring TypeScript type errors
5. You want to create a TypeScript function multiplyAll that accepts any number of numeric arguments and returns their product. Which is the best way to type the rest parameter and implement this function?
hard
A. function multiplyAll(nums: number[]): number { return nums.reduce((a, b) => a * b); }
B. function multiplyAll(...nums: number[]): number { return nums.reduce((a, b) => a * b, 1); }
C. function multiplyAll(...nums: number): number { return nums.reduce((a, b) => a * b, 1); }
D. function multiplyAll(...nums: any[]): any { return nums.reduce((a, b) => a * b, 0); }
Solution
Step 1: Choose correct rest parameter typing
The rest parameter must be typed as number[] to accept multiple numbers.
Step 2: Implement product with correct initial value
Use reduce with initial value 1 to multiply all numbers correctly.
Step 3: Evaluate options
function multiplyAll(...nums: number[]): number { return nums.reduce((a, b) => a * b, 1); } matches correct typing and logic. function multiplyAll(nums: number[]): number { return nums.reduce((a, b) => a * b); } lacks rest parameter. function multiplyAll(...nums: number): number { return nums.reduce((a, b) => a * b, 1); } has wrong type. function multiplyAll(...nums: any[]): any { return nums.reduce((a, b) => a * b, 0); } uses any and wrong initial value.
Final Answer:
function multiplyAll(...nums: number[]): number { return nums.reduce((a, b) => a * b, 1); } -> Option B
Quick Check:
Rest parameter typed number[] and reduce with 1 = correct [OK]
Hint: Rest parameter as number[] and reduce with 1 for product [OK]
Common Mistakes:
Using single number type instead of array for rest
Forgetting initial value in reduce
Using any type losing type safety
Starting reduce with 0 causing product always 0
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.