<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?
AThe type of instances created by MyClass
BThe type of the MyClass constructor function
CThe static properties of MyClass
DThe interface implemented by MyClass
✗ Incorrect
InstanceType extracts the instance type from a class constructor type.
Can InstanceType be used with a function type?
AYes, always
BYes, but only with arrow functions
CNo, only with class constructors
DOnly if the function returns an object
✗ 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?
Anew
Btypeof
Cinstanceof
Dclass
✗ Incorrect
You use typeof to get the type of the class constructor.
If you have class Dog { bark() {} }, what type does InstanceType have?
AAn object with a <code>bark</code> method
BThe Dog class itself
CA function type
DUndefined
✗ Incorrect
The instance type includes the bark method defined in the class.
What error occurs if you use InstanceType on a non-constructor type?
ASyntax error
BNo error, it works fine
CRuntime error
DTypeScript error: Type is not a constructor
✗ 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.
Practice
(1/5)
1. What does the TypeScript utility type InstanceType do?
easy
A. Creates a new class instance
B. Extracts the type of an instance created by a class
C. Defines a type alias for primitive types
D. Converts a type to a string
Solution
Step 1: Understand the purpose of InstanceType
InstanceType is a utility type that extracts the type of an object created by a class constructor.
Step 2: Compare options with the definition
Only Extracts the type of an instance created by a class correctly describes this behavior. Options B, C, and D describe unrelated actions.
Final Answer:
Extracts the type of an instance created by a class -> Option B
Quick Check:
InstanceType extracts instance type = A [OK]
Hint: Remember: InstanceType gets the object type from a class [OK]
Common Mistakes:
Confusing InstanceType with creating instances
Thinking it converts types to strings
Assuming it defines primitive types
2. Which of the following is the correct syntax to use InstanceType with a class named Car?
easy
A. type CarInstance = InstanceType;
B. type CarInstance = InstanceType;
C. type CarInstance = InstanceType;
D. type CarInstance = InstanceType<Car>;
Solution
Step 1: Recall the syntax of InstanceType
InstanceType expects a constructor type, so we pass typeof Car to get the class constructor type.
Step 2: Analyze each option
type CarInstance = InstanceType; uses typeof Car, which is correct. type CarInstance = InstanceType<Car>; passes the class itself, which is a value, not a type. Options A, B, and C use invalid syntax.
Final Answer:
type CarInstance = InstanceType; -> Option A
Quick Check:
Use typeof with class name inside InstanceType for correct syntax = D [OK]
Hint: Use typeof ClassName inside InstanceType for correct syntax [OK]
Common Mistakes:
Passing class name directly without typeof
Using parentheses after class name
Trying to instantiate inside InstanceType
3. Given the code below, what is the type of myDog?
class Dog {
name: string;
constructor(name: string) {
this.name = name;
}
}
let myDog: InstanceType;
myDog = new Dog('Buddy');
medium
A. string
B. { name: string; }
C. typeof Dog
D. Dog
Solution
Step 1: Understand what InstanceType means
It extracts the instance type created by the Dog class constructor, which is the class Dog itself.
Step 2: Match the type of myDog
myDog is typed as an instance of Dog, so its type is Dog (the class instance type). Dog matches this exactly.
Final Answer:
Dog -> Option D
Quick Check:
InstanceType = Dog instance type = A [OK]
Hint: InstanceType equals the class instance type [OK]
Common Mistakes:
Confusing instance type with constructor type
Choosing typeof Class instead of instance
Picking object shape instead of class type
4. Identify the error in the following TypeScript code using InstanceType:
class Cat {
age: number;
constructor(age: number) {
this.age = age;
}
}
type CatInstance = InstanceType<Cat>;
const kitty: CatInstance = new Cat(3);
medium
A. Missing typeof before Cat in InstanceType
B. Cannot assign new Cat(3) to kitty
C. Cat class must have a method
D. InstanceType cannot be used with classes
Solution
Step 1: Check the usage of InstanceType
InstanceType requires a constructor type, so the class name must be prefixed with typeof.
Step 2: Identify the error in the code
The code uses InstanceType<Cat> instead of InstanceType, causing a type error.
Final Answer:
Missing typeof before Cat in InstanceType -> Option A
Quick Check:
InstanceType needs typeof ClassName = C [OK]
Hint: Always use typeof with class name inside InstanceType [OK]
Common Mistakes:
Omitting typeof before class name
Thinking InstanceType works on instances
Assuming InstanceType works without constructor type
5. You have two classes, Circle and Square. You want to create a function that accepts an instance of either class using InstanceType. Which of the following is the correct way to type the parameter shape?
class Circle {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
}
class Square {
side: number;
constructor(side: number) {
this.side = side;
}
}
function drawShape(shape: ???) {
// drawing logic
}
hard
A. Circle | Square
B. typeof Circle | typeof Square
C. InstanceType | InstanceType
D. InstanceType
Solution
Step 1: Understand the goal
The function should accept instances of Circle or Square, so the parameter type must represent instances, not classes or constructors.
Step 2: Analyze each option
InstanceType | InstanceType correctly uses InstanceType and InstanceType combined with a union to represent instances of both classes. Circle | Square uses class names directly, which are types of instances but does not use InstanceType. typeof Circle | typeof Square uses constructor types, which is incorrect for instances. InstanceType tries to use InstanceType on a union of classes, which is invalid.
Final Answer:
InstanceType | InstanceType -> Option C
Quick Check:
Use InstanceType with typeof for each class, combine with | for union [OK]
Hint: Use InstanceType for each, join with | for multiple classes [OK]
Common Mistakes:
Using class names without InstanceType
Using typeof Class instead of InstanceType
Trying InstanceType on union of classes directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.