0
0
Typescriptprogramming~10 mins

Explicit 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 explicitly declare the type of the variable age as number.

Typescript
let age: [1] = 25;
Drag options to blanks, or click blank then click option'
Aany
Bstring
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type for a numeric value.
Omitting the type annotation.
2fill in blank
medium

Complete the code to explicitly declare the type of the function parameter name as string.

Typescript
function greet(name: [1]) {
  return `Hello, ${name}!`;
}
Drag options to blanks, or click blank then click option'
Anumber
Bboolean
Cstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using number type for a text parameter.
Not specifying any type.
3fill in blank
hard

Fix the error by explicitly typing the variable isActive as a boolean.

Typescript
let isActive: [1] = true;
Drag options to blanks, or click blank then click option'
Aany
Bboolean
Cstring
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Typing isActive as string or number.
Leaving out the type annotation.
4fill in blank
hard

Fill both blanks to explicitly type the array scores as an array of numbers and the variable total as a number.

Typescript
let scores: [1] = [10, 20, 30];
let total: [2] = 60;
Drag options to blanks, or click blank then click option'
Anumber[]
Bstring[]
Cnumber
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Typing scores as string[].
Typing total as string or any.
5fill in blank
hard

Fill all three blanks to explicitly type the object person with name as string, age as number, and isStudent as boolean.

Typescript
let person: { name: [1]; age: [2]; isStudent: [3] } = {
  name: "Alice",
  age: 22,
  isStudent: false
};
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types for properties.
Using any instead of specific types.