0
0
Typescriptprogramming~10 mins

Typeof operator in type context 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 same type as another variable using typeof.

Typescript
let name = "Alice";
let userName: [1] = "Bob";
Drag options to blanks, or click blank then click option'
Atypeof name
Btypeof "name"
Ctype name
Dtype of name
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'type name' instead of 'typeof name'.
Putting quotes around the variable name inside typeof.
2fill in blank
medium

Complete the code to declare a function parameter type using typeof operator on a variable.

Typescript
const age = 30;
function printAge(value: [1]) {
  console.log(value);
}
Drag options to blanks, or click blank then click option'
Atypeof 30
BAgeType
Ctypeof age
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using the literal type 'number' instead of typeof operator.
Using typeof with a value like 30 instead of the variable.
3fill in blank
hard

Fix the error by completing the type alias using typeof operator on a variable.

Typescript
const isActive = true;
type ActiveStatus = [1];
Drag options to blanks, or click blank then click option'
Atypeof isActive
Bboolean
Ctypeof true
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using the literal type 'boolean' instead of typeof operator.
Using 'Boolean' which is the constructor, not the type.
4fill in blank
hard

Fill both blanks to create a type alias for the type of a variable and use it in a function parameter.

Typescript
const score = 100;
type ScoreType = [1];
function printScore(value: [2]) {
  console.log(value);
}
Drag options to blanks, or click blank then click option'
Atypeof score
Bnumber
CScoreType
Dtypeof 100
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' directly instead of the type alias.
Using typeof 100 instead of typeof score.
5fill in blank
hard

Fill all three blanks to declare a variable, create a type alias using typeof, and use it in a function parameter.

Typescript
const isEnabled = false;
type EnabledType = [1];
let flag: [2] = false;
function toggle(value: [3]) {
  console.log(value);
}
Drag options to blanks, or click blank then click option'
Atypeof isEnabled
BEnabledType
Cboolean
Dtypeof false
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'boolean' directly instead of the type alias.
Using typeof false instead of typeof isEnabled.