0
0
Typescriptprogramming~10 mins

How assignment compatibility is checked in Typescript - Interactive 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 type number.

Typescript
let age: [1] = 30;
Drag options to blanks, or click blank then click option'
Anumber
Bstring
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type when assigning a number.
Using boolean type for numeric values.
2fill in blank
medium

Complete the code to assign a string value to a variable declared as string.

Typescript
let name: string = [1];
Drag options to blanks, or click blank then click option'
A25
B"Alice"
Ctrue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a number or boolean to a string variable.
Forgetting to use quotes around text.
3fill in blank
hard

Fix the error by completing the code to assign a compatible type.

Typescript
let isActive: boolean = [1];
Drag options to blanks, or click blank then click option'
Afalse
Bnull
C1
D"true"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "true" instead of boolean true.
Using numbers like 1 or 0 for boolean.
4fill in blank
hard

Fill both blanks to create a compatible assignment between two interfaces.

Typescript
interface Person { name: string; age: number; }
interface Employee extends Person { [1]: string; }
let emp: Employee = { name: "Bob", age: 25, [2]: "Manager" };
Drag options to blanks, or click blank then click option'
Aposition
Bsalary
Ddepartment
Attempts:
3 left
💡 Hint
Common Mistakes
Using different property names in interface and object.
Using a property not declared in the interface.
5fill in blank
hard

Fill all three blanks to complete a function that checks assignment compatibility with union types.

Typescript
function printId(id: [1]) {
  if (typeof id === [2]) {
    console.log("ID is a number: " + id);
  } else if (typeof id === [3]) {
    console.log("ID is a string: " + id);
  }
}
Drag options to blanks, or click blank then click option'
Anumber | string
B"number"
C"string"
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect type strings in typeof checks.
Not matching the union type in the function parameter.