0
0
Typescriptprogramming~10 mins

Rest parameters with types in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function with rest parameters typed as numbers.

Typescript
function sum(...numbers: [1]) { return numbers.reduce((a, b) => a + b, 0); }
Drag options to blanks, or click blank then click option'
Astring[]
Bnumber[]
Cany
Dboolean[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' instead of a specific type.
Forgetting to use array syntax '[]' for rest parameters.
2fill in blank
medium

Complete the code to type the rest parameter as an array of strings.

Typescript
function greetAll(...greetings: [1]) { greetings.forEach(greet => console.log(greet)); }
Drag options to blanks, or click blank then click option'
Astring[]
Bstring
CArray<string>
Dany[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' instead of 'string[]'.
Using 'any[]' which is less specific.
3fill in blank
hard

Fix the error by correctly typing the rest parameter as an array of booleans.

Typescript
function checkFlags(...flags: [1]) { return flags.every(flag => flag === true); }
Drag options to blanks, or click blank then click option'
Aany
BArray<boolean>
Cboolean
Dboolean[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'boolean' instead of 'boolean[]'.
Using 'any' which is not specific.
4fill in blank
hard

Fill both blanks to declare a function with rest parameters typed as an array of numbers and return their product.

Typescript
function multiplyAll(...[1]: [2]) { return [1].reduce((a, b) => a * b, 1); }
Drag options to blanks, or click blank then click option'
Anums
Bnumber[]
Cvalues
Dstring[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string array type instead of number array.
Forgetting to name the rest parameter.
5fill in blank
hard

Fill all three blanks to declare a function with rest parameters typed as strings, filter those longer than 3 characters, and return the filtered array.

Typescript
function filterLongStrings(...[1]: [2]): [2] { return [1].filter(str => str.length > 3); }
Drag options to blanks, or click blank then click option'
Awords
Bstring[]
Cnumber[]
Dany[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number[]' or 'any[]' instead of 'string[]'.
Mismatching parameter and return types.