0
0
Typescriptprogramming~10 mins

Parameter type annotations 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 add a type annotation for the parameter name.

Typescript
function greet(name: [1]) {
  return `Hello, ${name}!`;
}
Drag options to blanks, or click blank then click option'
Astring
Bany
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or boolean instead of string.
Omitting the type annotation.
2fill in blank
medium

Complete the code to add a type annotation for the parameter age.

Typescript
function setAge(age: [1]) {
  console.log(`Age set to ${age}`);
}
Drag options to blanks, or click blank then click option'
Aboolean
Bnumber
Cstring
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using string instead of number.
Using boolean or object incorrectly.
3fill in blank
hard

Fix the error by adding the correct type annotation for the parameter isActive.

Typescript
function setActive(isActive: [1]) {
  if (isActive) {
    console.log('Active');
  } else {
    console.log('Inactive');
  }
}
Drag options to blanks, or click blank then click option'
Aany
Bstring
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or number instead of boolean.
Omitting the type annotation.
4fill in blank
hard

Fill both blanks to add type annotations for parameters title and pages.

Typescript
function createBook(title: [1], pages: [2]) {
  return `${title} has ${pages} pages.`;
}
Drag options to blanks, or click blank then click option'
Astring
Bboolean
Cnumber
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the types between title and pages.
Using boolean or object incorrectly.
5fill in blank
hard

Fill all three blanks to add type annotations for parameters firstName, lastName, and age.

Typescript
function personInfo(firstName: [1], lastName: [2], age: [3]) {
  return `${firstName} ${lastName} is ${age} years old.`;
}
Drag options to blanks, or click blank then click option'
Anumber
Bstring
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the types for names and age.
Using boolean for any parameter.