0
0
Typescriptprogramming~10 mins

The any type and why to avoid it 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 with the 'any' type.

Typescript
let data: [1];
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 'any' when you want no type checking.
2fill in blank
medium

Complete the code to assign a string value to a variable typed as 'any'.

Typescript
let value: any = [1];
Drag options to blanks, or click blank then click option'
Atrue
B'hello'
C42
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string values.
3fill in blank
hard

Fix the error in the code by choosing the correct type instead of 'any'.

Typescript
function greet(name: [1]) {
  return 'Hello, ' + name;
}
Drag options to blanks, or click blank then click option'
Astring
Bboolean
Cnumber
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' loses type safety and can cause bugs.
4fill in blank
hard

Fill both blanks to create a typed object instead of using 'any'.

Typescript
let user: [1] = {
  name: 'Alice',
  age: [2]
};
Drag options to blanks, or click blank then click option'
A{ name: string; age: number }
Bany
C30
D'30'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' loses type safety.
Assigning age as a string instead of a number.
5fill in blank
hard

Fill all three blanks to avoid 'any' and use proper types in this function.

Typescript
function multiply(a: [1], b: [2]): [3] {
  return a * b;
}
Drag options to blanks, or click blank then click option'
Anumber
Bstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' loses type safety.
Using 'string' for parameters causes errors.