0
0
Typescriptprogramming~20 mins

Why instanceof fails on interfaces in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Instanceof Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does instanceof fail with interfaces in TypeScript?

Consider the following TypeScript code snippet:

interface Animal { name: string; }
class Dog implements Animal { name: string; constructor(name: string) { this.name = name; } }
const pet: Animal = new Dog('Buddy');
console.log(pet instanceof Animal);

What will be the output and why?

ATypeError, because pet is not a class instance
Bfalse, because interfaces do not exist at runtime and instanceof checks the prototype chain
CSyntaxError, because instanceof cannot be used with interfaces
Dtrue, because pet is an instance of a class that implements Animal
Attempts:
2 left
💡 Hint

Think about what instanceof checks at runtime and what interfaces represent in TypeScript.

Predict Output
intermediate
2:00remaining
Output of instanceof with class and interface

What is the output of this TypeScript code?

interface Vehicle { wheels: number; }
class Car implements Vehicle { wheels = 4; }
const myCar = new Car();
console.log(myCar instanceof Car);
console.log(myCar instanceof Vehicle);
A
true
false
B
true
true
C
false
false
D
false
true
Attempts:
2 left
💡 Hint

Remember that interfaces do not exist at runtime.

🔧 Debug
advanced
2:00remaining
Why does this instanceof check always fail?

Look at this TypeScript code:

interface Shape { area(): number; }
class Circle implements Shape {
  radius: number;
  constructor(radius: number) { this.radius = radius; }
  area() { return Math.PI * this.radius * this.radius; }
}
const shape: Shape = new Circle(5);
if (shape instanceof Shape) {
  console.log('It is a Shape');
} else {
  console.log('Not a Shape');
}

Why does it always print 'Not a Shape'?

ABecause Shape is an interface and does not exist at runtime, so instanceof always fails
BBecause Circle does not extend Shape class, so instanceof returns false
CBecause area method is missing in the object
DBecause shape is not an object
Attempts:
2 left
💡 Hint

Think about what instanceof checks and what interfaces represent.

📝 Syntax
advanced
2:00remaining
Which code snippet causes a runtime error due to instanceof misuse?

Which of the following TypeScript code snippets will cause a runtime error when executed?

A
interface Person { name: string; }
const p: Person = { name: 'Alice' };
console.log(p instanceof Person);
B
class Person { name: string; constructor(name: string) { this.name = name; } }
const p = new Person('Alice');
console.log(p instanceof Person);
C
const p = { name: 'Alice' };
console.log(p instanceof Object);
D
const p = null;
console.log(p instanceof Object);
Attempts:
2 left
💡 Hint

Consider what values can be used with instanceof without causing errors.

🚀 Application
expert
3:00remaining
How to check if an object implements an interface at runtime?

Since instanceof does not work with interfaces in TypeScript, which approach below correctly checks if an object implements the Logger interface at runtime?

interface Logger { log(message: string): void; }
AUse <code>typeof obj === 'Logger'</code> to check the type
BUse <code>obj instanceof Logger</code> to check if it implements Logger
CUse <code>if ('log' in obj && typeof obj.log === 'function')</code> to check for the method
DUse <code>obj.constructor.name === 'Logger'</code> to check the class name
Attempts:
2 left
💡 Hint

Interfaces do not exist at runtime, so you must check properties manually.