0
0
Typescriptprogramming~10 mins

Why type annotations are needed in Typescript - Test Your Understanding

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

Complete the code to declare a variable with a number type annotation.

Typescript
let age: [1] = 30;
Drag options to blanks, or click blank then click option'
Aany
Bstring
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' instead of 'number' causes type errors.
Omitting the type annotation can lead to implicit 'any' type.
2fill in blank
medium

Complete the function parameter with the correct type annotation.

Typescript
function greet(name: [1]) { console.log(`Hello, ${name}!`); }
Drag options to blanks, or click blank then click option'
Anumber
Bobject
Cstring
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' or 'boolean' causes errors when passing text.
Not specifying a type can lead to unexpected behavior.
3fill in blank
hard

Fix the error by adding the correct return type annotation to the function.

Typescript
function add(a: number, b: number): [1] { return a + b; }
Drag options to blanks, or click blank then click option'
Anumber
Bboolean
Cstring
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' means no value is returned, causing errors here.
Using 'string' or 'boolean' does not match the actual return value.
4fill in blank
hard

Fill both blanks to create a typed object with a name and age.

Typescript
const person: { name: [1]; age: [2] } = { name: 'Alice', age: 25 };
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types causes type errors.
Using 'any' loses the benefit of type checking.
5fill in blank
hard

Fill all three blanks to type a function that returns a boolean indicating if a number is positive.

Typescript
function isPositive(num: [1]): [2] { return num [3] 0; }
Drag options to blanks, or click blank then click option'
Astring
Bboolean
C>
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types for parameters or return values.
Using incorrect comparison operators.