The instanceof operator checks if an object is created from a specific class, but interfaces do not exist in the final JavaScript code. So, instanceof cannot check interfaces.
Why instanceof fails on interfaces in Typescript
object instanceof ClassNameinstanceof works only with classes or constructor functions.
Interfaces are only used by TypeScript during development and disappear after compiling.
pet is created from the Dog class.class Dog {} const pet = new Dog(); console.log(pet instanceof Dog); // true
instanceof cannot be used with interfaces because they do not exist at runtime.interface Animal {
name: string;
}
const pet = { name: 'Buddy' };
// console.log(pet instanceof Animal); // Error: 'Animal' only exists in type spaceThis program shows that instanceof works with the Dog class but not with the Animal interface.
interface Animal {
name: string;
}
class Dog implements Animal {
constructor(public name: string) {}
}
const pet = new Dog('Buddy');
console.log(pet instanceof Dog); // true
// The following line would cause an error if uncommented:
// console.log(pet instanceof Animal);Interfaces are only for TypeScript's type checking and do not exist in the JavaScript output.
To check if an object matches an interface, check for the presence of required properties instead.
Use instanceof only with classes or constructor functions.
instanceof checks if an object is created from a class, not if it matches an interface.
Interfaces disappear after TypeScript compiles to JavaScript, so instanceof cannot see them.
To check interface-like shapes at runtime, check object properties manually.
Practice
instanceof operator fail when used to check if an object implements a TypeScript interface?Solution
Step 1: Understand what
Theinstanceofchecksinstanceofoperator 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, soinstanceofcannot check them.Final Answer:
Because interfaces do not exist at runtime in JavaScript. -> Option CQuick Check:
Interfaces disappear at runtime = A [OK]
instanceof fails [OK]- Thinking
instanceofworks with interfaces - Confusing interfaces with classes
- Assuming interfaces exist at runtime
- Using
instanceofwith primitives
Person in TypeScript?Solution
Step 1: Recall
The right side ofinstanceofsyntaxinstanceofmust be a class or constructor function, not a string or interface.Step 2: Identify valid usage
Onlyobj instanceof Personis valid syntax to check ifobjis created from classPerson.Final Answer:
if (obj instanceof Person) { } -> Option AQuick Check:
Right operand must be class/constructor = C [OK]
instanceof [OK]- Using interface names with
instanceof - Putting class name in quotes
- Using object literals on right side
- Confusing type and value syntax
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?
Solution
Step 1: Check
Sincepet instanceof Dogpetwas created withnew Dog(), this returnstrue.Step 2: Check
pet instanceof AnimalAnimalis an interface and does not exist at runtime, so this returnsfalse.Final Answer:
true\nfalse -> Option DQuick Check:
Class instance true, interface instance false = A [OK]
instanceof, interfaces do not [OK]- Expecting interface check to return true
- Confusing interface with class
- Assuming interfaces exist at runtime
- Misreading output order
interface Vehicle { wheels: number }
function isVehicle(obj: any) {
return obj instanceof Vehicle;
}Why does this code cause an error or unexpected behavior?
Solution
Step 1: Identify the use of
The code usesinstanceofwith interfaceinstanceofwithVehicle, which is an interface.Step 2: Understand interface runtime behavior
Interfaces do not exist at runtime, soinstanceof Vehicleis invalid and causes an error or always returns false.Final Answer:
BecauseVehicleis an interface and cannot be used withinstanceof. -> Option AQuick Check:
Interfaces can't be checked withinstanceof= D [OK]
instanceof with interfaces, only classes [OK]- Trying to check interfaces with
instanceof - Ignoring runtime absence of interfaces
- Assuming
instanceofworks with types - Misunderstanding error cause
Person { name: string; age: number; }. Since instanceof does not work with interfaces, which approach is best?Solution
Step 1: Understand interface runtime absence
Interfaces do not exist at runtime, soinstanceofortypeofcannot 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 BQuick Check:
Manual property checks replaceinstanceoffor interfaces = B [OK]
- Using
instanceofwith interfaces - Assuming
typeofworks for interfaces - Converting interfaces to classes unnecessarily
- Ignoring runtime type checks
Ready to prove it?
Pass the test and this pattern moves unknown
