0
0
Typescriptprogramming~20 mins

InstanceType type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
InstanceType Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using InstanceType?
Consider the following TypeScript code using InstanceType. What will be logged to the console?
Typescript
class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  speak() {
    return `${this.name} makes a noise.`;
  }
}

type AnimalInstance = InstanceType<typeof Animal>;

const pet: AnimalInstance = new Animal('Dog');
console.log(pet.speak());
A"Dog makes a noise."
B"Animal makes a noise."
CTypeError at runtime
DCompilation error due to InstanceType misuse
Attempts:
2 left
💡 Hint
InstanceType extracts the instance type from a constructor function type.
🧠 Conceptual
intermediate
1:30remaining
What does InstanceType do in TypeScript?
Choose the best description of what InstanceType<T> does when T is a constructor type.
AConverts a class type to a primitive type.
BExtracts the type of the constructor function itself, not the instance.
CCreates a new instance of type <code>T</code> at runtime.
DExtracts the type of the instance created by the constructor type <code>T</code>.
Attempts:
2 left
💡 Hint
Think about what a constructor function returns when called with new.
Predict Output
advanced
2:00remaining
What is the type of variable 'obj' here?
Given this TypeScript code, what is the type of obj?
Typescript
class Person {
  constructor(public name: string, public age: number) {}
}

type PersonConstructor = typeof Person;

type PersonInstance = InstanceType<PersonConstructor>;

const obj: PersonInstance = { name: 'Alice', age: 30 };
AAn instance of <code>Person</code> with methods only, no properties.
BA constructor function type, not an object.
CAn object with properties <code>name</code> (string) and <code>age</code> (number).
DA compilation error because <code>obj</code> is not created with <code>new</code>.
Attempts:
2 left
💡 Hint
InstanceType extracts the instance type, which includes properties.
🔧 Debug
advanced
1:30remaining
What error does this code produce?
Examine this TypeScript code using InstanceType. What error will the compiler show?
Typescript
type NotAConstructor = {
  foo: string;
};

type Result = InstanceType<NotAConstructor>;
AType 'NotAConstructor' does not satisfy the constraint 'new (...args: any) => any'.
BNo error, Result is type 'any'.
CRuntime error when accessing Result.
DSyntax error due to missing constructor.
Attempts:
2 left
💡 Hint
InstanceType requires a constructor type as input.
🧠 Conceptual
expert
2:30remaining
Why use InstanceType instead of directly using a class type?
Which reason best explains why you might use InstanceType<typeof SomeClass> instead of just using SomeClass as a type?
ATo create a new instance of the class at runtime automatically.
BTo get the instance type from a constructor type when the class type is not directly accessible or to work with constructor signatures generically.
CTo convert the class type into a primitive type for optimization.
DTo enforce that the class has no static methods.
Attempts:
2 left
💡 Hint
Think about generic programming and working with constructor types.