0
0
Typescriptprogramming~20 mins

Rest parameters with types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rest Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function using typed rest parameters
What is the output of this TypeScript code when calling sumAll(1, 2, 3, 4)?
Typescript
function sumAll(...nums: number[]): number {
  return nums.reduce((a, b) => a + b, 0);
}
console.log(sumAll(1, 2, 3, 4));
ATypeError at runtime
B10
Cundefined
D1234
Attempts:
2 left
💡 Hint
The rest parameter collects all arguments into an array of numbers.
Predict Output
intermediate
2:00remaining
Output of function with mixed typed rest parameters
What will this TypeScript code output when calling concatStrings('a', 'b', 'c')?
Typescript
function concatStrings(separator: string, ...strings: string[]): string {
  return strings.join(separator);
}
console.log(concatStrings('-', 'a', 'b', 'c'));
A"a-b-c"
B"-abc"
C"abc-"
DTypeError at runtime
Attempts:
2 left
💡 Hint
The first argument is the separator, the rest are joined with it.
🔧 Debug
advanced
2:00remaining
Identify the error with rest parameter typing
What error does this TypeScript code produce?
Typescript
function multiplyAll(...nums: number): number {
  return nums.reduce((a, b) => a * b, 1);
}
ATypeError: nums.reduce is not a function
BRuntime error: missing initial value in reduce
CNo error, outputs product of numbers
DSyntaxError: Rest parameter must be an array type
Attempts:
2 left
💡 Hint
Check the type of the rest parameter declaration.
Predict Output
advanced
2:00remaining
Output of function with tuple typed rest parameters
What is the output of this TypeScript code?
Typescript
function formatName(first: string, last: string, ...titles: string[]): string {
  return `${first} ${last} ${titles.join(' ')}`.trim();
}
console.log(formatName('John', 'Doe', 'PhD', 'MD'));
A"John Doe PhD MD"
B"John Doe"
C"John Doe PhD, MD"
DTypeError at runtime
Attempts:
2 left
💡 Hint
The rest parameter collects all extra titles as an array.
🧠 Conceptual
expert
2:00remaining
TypeScript rest parameter with tuple types
Given this function signature, what is the type of the rest parameter args?
function example(...args: [number, string, boolean]): void;
AAn array of numbers only
BAn array of any length containing number, string, or boolean in any order
CA tuple with exactly three elements: number, string, boolean
DA single argument of type array containing number, string, boolean
Attempts:
2 left
💡 Hint
Tuple types specify fixed length and types in order.