Recall & Review
beginner
What does the
InstanceType type do in TypeScript?<p><code>InstanceType</code> takes a class type and returns the type of instances created by that class.</p>Click to reveal answer
beginner
How do you use <code>InstanceType</code> with a class named <code>Car</code>?You write InstanceType<typeof Car> to get the type of an object created by new Car().
Click to reveal answer
beginner
Can
InstanceType be used with interfaces or only classes?<p><code>InstanceType</code> works only with class constructors, not interfaces.</p>Click to reveal answer
intermediate
What happens if you use
InstanceType on a type that is not a constructor?TypeScript will give an error because InstanceType expects a constructor type.
Click to reveal answer
beginner
Why is
InstanceType useful in TypeScript?<p>It helps you get the instance type from a class type, so you can write safer and clearer code without repeating types.</p>Click to reveal answer
What does
InstanceType return?✗ Incorrect
InstanceType extracts the instance type from a class constructor type.
Can
InstanceType be used with a function type?✗ Incorrect
InstanceType requires a class constructor type, not a regular function type.
What keyword do you use inside
InstanceType to refer to a class type?✗ Incorrect
You use typeof to get the type of the class constructor.
If you have
class Dog { bark() {} }, what type does InstanceType have?✗ Incorrect
The instance type includes the bark method defined in the class.
What error occurs if you use
InstanceType on a non-constructor type?✗ Incorrect
TypeScript expects a constructor type and will error if given something else.
Explain how
InstanceType helps when working with classes in TypeScript.Think about how you get the type of an object created by a class.
You got /4 concepts.
Describe a situation where using
InstanceType would prevent a type error in your code.Consider when you want to refer to the type of an object created by a class without writing it yourself.
You got /4 concepts.