0
0
Typescriptprogramming~10 mins

Parameters type 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 parameter with type number.

Typescript
function add(a: [1]) { return a + 10; }
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type causes a type error when adding a number.
Using boolean is incorrect for numeric operations.
2fill in blank
medium

Complete the code to declare a function parameter with type string.

Typescript
function greet(name: [1]) { return `Hello, ${name}!`; }
Drag options to blanks, or click blank then click option'
Astring
Bobject
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or boolean causes errors when concatenating strings.
3fill in blank
hard

Fix the error in the function parameter type to accept an array of numbers.

Typescript
function sum(numbers: [1]) { return numbers.reduce((a, b) => a + b, 0); }
Drag options to blanks, or click blank then click option'
Anumber
Bstring[]
Cnumber[]
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using number instead of number[] causes errors because a single number is expected, not an array.
4fill in blank
hard

Fill both blanks to declare a function with two parameters: a string and a boolean.

Typescript
function checkStatus(name: [1], isActive: [2]) { return `${name} is ${isActive ? 'active' : 'inactive'}`; }
Drag options to blanks, or click blank then click option'
Astring
Bboolean
Cnumber
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the types or using number instead of boolean.
5fill in blank
hard

Fill all three blanks to declare a function with parameters: a string, a number, and an optional boolean.

Typescript
function createUser(name: [1], age: [2], isAdmin?: [3]) { return { name, age, isAdmin }; }
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the optional parameter syntax or using wrong types.