0
0
Typescriptprogramming~10 mins

Why instanceof fails on interfaces in Typescript - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why instanceof fails on interfaces
Create Interface
Create Class implementing Interface
Create Object from Class
Use instanceof to check Interface
Fails: instanceof only works with Classes
Use alternative: 'in' operator or user-defined type guards
This flow shows that instanceof works only with classes, not interfaces, so checking interface types with instanceof fails.
Execution Sample
Typescript
interface IAnimal { speak(): void; }
class Dog implements IAnimal {
  speak() { console.log('Woof'); }
}
const pet = new Dog();
console.log(pet instanceof IAnimal);
This code tries to check if pet is an instance of interface IAnimal using instanceof, which fails.
Execution Table
StepActionEvaluationResult
1Define interface IAnimalN/AInterface created, no runtime code
2Define class Dog implementing IAnimalN/AClass Dog created with speak method
3Create object pet = new Dog()N/Apet is an instance of Dog
4Evaluate pet instanceof IAnimalpet instanceof IAnimalError or false, because IAnimal is not a class
5Use alternative check: 'speak' in pet'speak' in pettrue, pet has speak method
💡 instanceof fails because interfaces do not exist at runtime, only classes do
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5
petundefinedDog instanceN/A (instanceof check fails)Dog instance
Key Moments - 2 Insights
Why does 'pet instanceof IAnimal' fail even though Dog implements IAnimal?
Because interfaces are only a compile-time construct and do not exist in the JavaScript runtime, instanceof can only check against classes, not interfaces. See execution_table step 4.
How can I check if an object implements an interface at runtime?
You can check if the object has the required properties or methods using the 'in' operator or user-defined type guards, as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of 'pet instanceof IAnimal' at step 4?
Atrue
Bfalse or error
Cundefined
Dpet object
💡 Hint
Check execution_table row 4 under Result column
At which step does the object 'pet' get created as an instance of Dog?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 describing object creation
If you want to check if 'pet' has a 'speak' method, which step shows a working approach?
AStep 5
BStep 2
CStep 4
DStep 1
💡 Hint
See execution_table row 5 for alternative check using 'in' operator
Concept Snapshot
instanceof checks only work with classes at runtime
Interfaces are compile-time only and do not exist in JavaScript
Checking interface implementation with instanceof fails
Use 'in' operator or custom type guards to check interface shape
Classes create real objects; interfaces do not
Remember: instanceof needs a constructor function
Full Transcript
In TypeScript, interfaces are only used during compilation to check types. They do not exist in the JavaScript code that runs. The instanceof operator checks if an object was created by a specific class constructor. Since interfaces have no constructor at runtime, using instanceof with an interface always fails or returns false. Instead, to check if an object matches an interface, you can check if it has the required properties or methods using the 'in' operator or write custom type guard functions. This visual trace shows defining an interface, a class implementing it, creating an object, and then trying instanceof which fails. Finally, it shows a working alternative using the 'in' operator.