What does the instanceof operator check in TypeScript?
<code>instanceof</code> checks if an object is an instance of a specific class or constructor function at runtime.
Click to reveal answer
beginner
Why can't instanceof be used with interfaces in TypeScript?
Interfaces are only a compile-time construct in TypeScript and do not exist in the generated JavaScript code, so instanceof cannot check them at runtime.
Click to reveal answer
intermediate
How can you check if an object implements an interface in TypeScript?
You can check if an object implements an interface by verifying the presence of required properties or methods manually, often called 'duck typing'.
Click to reveal answer
intermediate
What is 'duck typing' in the context of TypeScript interfaces?
'Duck typing' means checking if an object has the necessary properties or methods to be considered of a certain type, rather than checking its actual class or constructor.
Click to reveal answer
intermediate
Can you use instanceof with classes that implement interfaces?
Yes, you can use instanceof with classes because they exist at runtime, but this only checks the class, not the interface itself.
Click to reveal answer
What does instanceof check in TypeScript?
AIf an object is an instance of a class or constructor
BIf an object implements an interface
CIf an object has certain properties
DIf an object is a primitive type
✗ Incorrect
instanceof works by checking the prototype chain of an object against a class or constructor function.
Why does instanceof fail with interfaces?
AInterfaces are primitive types
BInterfaces are removed during compilation and don't exist at runtime
CInterfaces are only for classes
DInterfaces are only used in JavaScript
✗ Incorrect
Interfaces are a TypeScript-only feature and do not exist in the JavaScript output, so instanceof cannot check them.
How can you check if an object matches an interface?
AUsing <code>new</code> keyword
BUsing <code>instanceof</code>
CUsing <code>typeof</code> operator
DChecking if required properties or methods exist
✗ Incorrect
You manually check if the object has the properties or methods defined by the interface, a technique called duck typing.
Which of these is true about interfaces in TypeScript?
AThey exist at runtime
BThey are compiled to JavaScript classes
CThey are only used during compile time for type checking
DThey can be checked with <code>instanceof</code>
✗ Incorrect
Interfaces are erased during compilation and only help with type checking during development.
Can instanceof check if an object is from a class that implements an interface?
AYes, it checks the class, not the interface
BNo, it only checks interfaces
CYes, it checks both class and interface
DNo, it only works with primitives
✗ Incorrect
instanceof checks the class or constructor function, but interfaces do not exist at runtime.
Explain why the instanceof operator does not work with interfaces in TypeScript.
Think about what exists in the final JavaScript code.
You got /4 concepts.
Describe how you can check if an object implements an interface in TypeScript without using instanceof.
Focus on what you can check on the object itself.
You got /4 concepts.
Practice
(1/5)
1. Why does the instanceof operator fail when used to check if an object implements a TypeScript interface?
easy
A. Because interfaces are only used for styling in TypeScript.
B. Because instanceof only works with primitive types.
C. Because interfaces do not exist at runtime in JavaScript.
D. Because instanceof requires a function as the right operand.
Solution
Step 1: Understand what instanceof checks
The instanceof operator checks if an object was created from a specific class or constructor function at runtime.
Step 2: Recognize interface behavior in TypeScript
Interfaces in TypeScript are only used during compilation and do not exist in the generated JavaScript code, so instanceof cannot check them.
Final Answer:
Because interfaces do not exist at runtime in JavaScript. -> Option C
Quick Check:
Interfaces disappear at runtime = A [OK]
Hint: Remember: interfaces vanish after compile, so instanceof fails [OK]
Common Mistakes:
Thinking instanceof works with interfaces
Confusing interfaces with classes
Assuming interfaces exist at runtime
Using instanceof with primitives
2. Which of the following is the correct syntax to check if an object is an instance of a class Person in TypeScript?
easy
A. if (obj instanceof Person) { }
B. if (obj instanceof 'Person') { }
C. if (obj instanceof InterfacePerson) { }
D. if (obj instanceof { name: string }) { }
Solution
Step 1: Recall instanceof syntax
The right side of instanceof must be a class or constructor function, not a string or interface.
Step 2: Identify valid usage
Only obj instanceof Person is valid syntax to check if obj is created from class Person.
Final Answer:
if (obj instanceof Person) { } -> Option A
Quick Check:
Right operand must be class/constructor = C [OK]
Hint: Use class name without quotes for instanceof [OK]
Common Mistakes:
Using interface names with instanceof
Putting class name in quotes
Using object literals on right side
Confusing type and value syntax
3. Consider this TypeScript code:
interface Animal { name: string }
class Dog implements Animal { constructor(public name: string) {} }
const pet = new Dog('Buddy');
console.log(pet instanceof Dog);
console.log(pet instanceof Animal);
What is the output?
medium
A. false\nfalse
B. true\ntrue
C. false\ntrue
D. true\nfalse
Solution
Step 1: Check pet instanceof Dog
Since pet was created with new Dog(), this returns true.
Step 2: Check pet instanceof Animal
Animal is an interface and does not exist at runtime, so this returns false.
Final Answer:
true\nfalse -> Option D
Quick Check:
Class instance true, interface instance false = A [OK]
Hint: Only classes work with instanceof, interfaces do not [OK]
Common Mistakes:
Expecting interface check to return true
Confusing interface with class
Assuming interfaces exist at runtime
Misreading output order
4. Given this code snippet:
interface Vehicle { wheels: number }
function isVehicle(obj: any) {
return obj instanceof Vehicle;
}
Why does this code cause an error or unexpected behavior?
medium
A. Because Vehicle is an interface and cannot be used with instanceof.
B. Because obj is not typed correctly.
C. Because instanceof requires a string argument.
D. Because Vehicle is not imported.
Solution
Step 1: Identify the use of instanceof with interface
The code uses instanceof with Vehicle, which is an interface.
Step 2: Understand interface runtime behavior
Interfaces do not exist at runtime, so instanceof Vehicle is invalid and causes an error or always returns false.
Final Answer:
Because Vehicle is an interface and cannot be used with instanceof. -> Option A
Quick Check:
Interfaces can't be checked with instanceof = D [OK]
Hint: Never use instanceof with interfaces, only classes [OK]
Common Mistakes:
Trying to check interfaces with instanceof
Ignoring runtime absence of interfaces
Assuming instanceof works with types
Misunderstanding error cause
5. You want to check at runtime if an object matches the shape of an interface Person { name: string; age: number; }. Since instanceof does not work with interfaces, which approach is best?
hard
A. Use instanceof Person anyway; it will work after compilation.
B. Manually check if object has required properties with correct types.
C. Convert the interface to a class and use instanceof.
D. Use typeof obj === 'Person' to check the type.
Solution
Step 1: Understand interface runtime absence
Interfaces do not exist at runtime, so instanceof or typeof cannot check them directly.
Step 2: Choose property checks for runtime validation
The best way is to manually check if the object has the required properties with the expected types.
Final Answer:
Manually check if object has required properties with correct types. -> Option B
Quick Check:
Manual property checks replace instanceof for interfaces = B [OK]
Hint: Check properties manually to mimic interface checks at runtime [OK]
Common Mistakes:
Using instanceof with interfaces
Assuming typeof works for interfaces
Converting interfaces to classes unnecessarily
Ignoring runtime type checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.