0
0
Typescriptprogramming~10 mins

Constructor parameter types 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 constructor parameter type for name.

Typescript
class Person {
  name: string;
  constructor(name: [1]) {
    this.name = name;
  }
}
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or boolean instead of string for the name parameter.
2fill in blank
medium

Complete the code to declare a constructor parameter type for age.

Typescript
class User {
  age: [1];
  constructor(age: number) {
    this.age = age;
  }
}
Drag options to blanks, or click blank then click option'
Astring
Bany
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type for age instead of number.
3fill in blank
hard

Fix the error in the constructor parameter type for isActive.

Typescript
class Account {
  isActive: boolean;
  constructor(isActive: [1]) {
    this.isActive = isActive;
  }
}
Drag options to blanks, or click blank then click option'
Aboolean
Bnumber
Cstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or number type for boolean properties.
4fill in blank
hard

Fill both blanks to declare constructor parameter types for title and pages.

Typescript
class Book {
  title: [1];
  pages: [2];
  constructor(title: string, pages: number) {
    this.title = title;
    this.pages = pages;
  }
}
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types between title and pages.
5fill in blank
hard

Fill all three blanks to declare constructor parameter types for username, email, and verified.

Typescript
class Member {
  username: [1];
  email: [2];
  verified: [3];
  constructor(username: string, email: string, verified: boolean) {
    this.username = username;
    this.email = email;
    this.verified = verified;
  }
}
Drag options to blanks, or click blank then click option'
Astring
Bboolean
Cnumber
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using number type for username or email.
Using string type for verified flag.