0
0
Typescriptprogramming~10 mins

InstanceType type 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 get the instance type of the class.

Typescript
class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}
type PersonInstance = [1]<typeof Person>;
Drag options to blanks, or click blank then click option'
AInstanceType
BReturnType
CParameters
DConstructorParameters
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReturnType instead of InstanceType
Using Parameters or ConstructorParameters which extract function parameters
2fill in blank
medium

Complete the code to create an instance of the class using the instance type.

Typescript
class Car {
  model: string;
  constructor(model: string) {
    this.model = model;
  }
}
type CarInstance = InstanceType<typeof Car>;
const myCar: [1] = new Car('Tesla');
Drag options to blanks, or click blank then click option'
ACar
Bstring
Ctypeof Car
DCarInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class type Car instead of the instance type alias
Using typeof Car which is the constructor type
3fill in blank
hard

Fix the error in the code by completing the type alias correctly.

Typescript
class Animal {
  species: string;
  constructor(species: string) {
    this.species = species;
  }
}
type AnimalInstance = [1];
Drag options to blanks, or click blank then click option'
AInstanceType<typeof Animal>
BReturnType
CInstanceType
Dtypeof Animal
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class type Animal directly instead of typeof Animal
Using ReturnType which is for functions
4fill in blank
hard

Fill both blanks to create a type alias for the instance type and declare a variable of that type.

Typescript
class Book {
  title: string;
  constructor(title: string) {
    this.title = title;
  }
}
type [1] = [2]<typeof Book>;
const myBook: BookInstance = new Book('1984');
Drag options to blanks, or click blank then click option'
ABookInstance
BInstanceType
CReturnType
DBookType
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReturnType instead of InstanceType
Naming the alias incorrectly
5fill in blank
hard

Fill all three blanks to create an instance type alias, declare a variable, and access a property.

Typescript
class User {
  username: string;
  constructor(username: string) {
    this.username = username;
  }
}
type [1] = [2]<typeof User>;
const user: UserInstance = new User('alice');
const name: string = user.[3];
Drag options to blanks, or click blank then click option'
AUserInstance
BInstanceType
Cusername
DuserName
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property name userName with uppercase N
Not using InstanceType with typeof User