0
0
Typescriptprogramming~10 mins

Why patterns matter for safety 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 safe type.

Typescript
let userName: [1] = "Alice";
Drag options to blanks, or click blank then click option'
Astring
Bany
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' type which disables safety checks.
2fill in blank
medium

Complete the code to define a function parameter with a safe type.

Typescript
function greet(name: [1]) { console.log(`Hello, ${name}!`); }
Drag options to blanks, or click blank then click option'
Anumber
Bboolean
Cstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' which can cause unexpected errors.
3fill in blank
hard

Fix the error by choosing the correct type for the variable.

Typescript
let age: [1] = "30";
Drag options to blanks, or click blank then click option'
Aboolean
Bnumber
Cany
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' type with a string value causes errors.
4fill in blank
hard

Fill both blanks to create a safe object type and assign it.

Typescript
type User = { name: [1]; age: [2]; };
const user: User = { name: "Bob", age: 25 };
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing types or using 'any' reduces safety.
5fill in blank
hard

Fill all three blanks to safely check a variable's type before using it.

Typescript
function printAge(age: [1] | [2]) {
  if (typeof age === [3]) {
    console.log(`Age is ${age}`);
  } else {
    console.log("Invalid age");
  }
}
Drag options to blanks, or click blank then click option'
Anumber
Bstring
C"number"
D"string"
Attempts:
3 left
💡 Hint
Common Mistakes
Using type names instead of string literals in typeof check.