0
0
Typescriptprogramming~10 mins

Type annotation on variables 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 variable age with type number.

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

Complete the code to declare a constant isActive with type boolean.

Typescript
const isActive: [1] = true;
Drag options to blanks, or click blank then click option'
Astring
Bboolean
Cnumber
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or number instead of boolean.
Forgetting to add the type annotation.
3fill in blank
hard

Fix the error in the code by adding the correct type annotation to names as an array of strings.

Typescript
let names: [1] = ["Alice", "Bob", "Charlie"];
Drag options to blanks, or click blank then click option'
Astring[]
Bstring
CArray<number>
Dnumber[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using string instead of string[].
Using number array types by mistake.
4fill in blank
hard

Fill both blanks to declare a variable user as an object with name as string and age as number.

Typescript
let user: [1] = { name: "John", age: 30 }; // user has name: string and age: number

// Type annotation for user is [2]
Drag options to blanks, or click blank then click option'
A{ name: string; age: number }
B{ name: number; age: string }
C{ name: string; age: string }
D{ name: number; age: number }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the types of name and age.
Using string type for age.
5fill in blank
hard

Fill all three blanks to declare a variable coordinates as a tuple with two numbers: x and y.

Typescript
let coordinates: [1] = [[2], [3]];
Drag options to blanks, or click blank then click option'
A[number, number]
B10
C20
D[string, string]
Attempts:
3 left
💡 Hint
Common Mistakes
Using array type instead of tuple.
Using strings instead of numbers.