0
0
Typescriptprogramming~10 mins

instanceof type guards in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - instanceof type guards
Start with variable
Check: variable instanceof Class?
NoElse branch
Yes
Inside if: variable treated as Class type
Use variable with Class properties/methods
End
The program checks if a variable is an instance of a class. If yes, it treats the variable as that class type inside the if block.
Execution Sample
Typescript
class Cat {
  meow() { return 'meow'; }
}

const pet = new Cat();

if (pet instanceof Cat) {
  console.log(pet.meow());
}
This code checks if pet is an instance of Cat, then calls the meow method safely.
Execution Table
StepActionEvaluationResult
1Create class CatN/AClass Cat defined
2Create pet = new Cat()pet instanceof Cattrue
3Check if pet instanceof CattrueEnter if block
4Call pet.meow()pet.meow()'meow' printed
5EndN/AProgram ends
💡 No exit from if because pet is instance of Cat
Variable Tracker
VariableStartAfter 1After 2Final
petundefinedCat instanceCat instanceCat instance
Key Moments - 2 Insights
Why can we call pet.meow() inside the if block?
Because the instanceof check (see step 3 in execution_table) confirms pet is a Cat, so TypeScript knows pet has the meow method.
What happens if pet is not an instance of Cat?
The if condition fails (step 3), so the code inside the if block does not run, preventing errors from calling meow on a wrong type.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of 'pet instanceof Cat' at step 2?
Aundefined
Bfalse
Ctrue
Derror
💡 Hint
Check the 'Evaluation' column at step 2 in execution_table
At which step does the program enter the if block?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Result' column in execution_table for step 3
If pet was not an instance of Cat, what would happen to the call pet.meow()?
AIt would be skipped because the if block is not entered
BIt would run normally
CIt would cause a runtime error
DIt would print undefined
💡 Hint
Refer to key_moments about what happens if instanceof check fails
Concept Snapshot
instanceof type guards check if a variable is an instance of a class.
Syntax: if (variable instanceof Class) { /* variable treated as Class */ }
Inside the if block, TypeScript knows the variable has Class properties/methods.
Prevents errors by narrowing types safely at runtime.
Full Transcript
This visual trace shows how instanceof type guards work in TypeScript. First, a class Cat is defined. Then, a variable pet is created as an instance of Cat. The program checks if pet is an instance of Cat using 'pet instanceof Cat'. Since this is true, the code inside the if block runs, allowing safe use of pet.meow(). The variable tracker shows pet starts undefined and becomes a Cat instance. Key moments explain why the method call is safe and what happens if the check fails. The quizzes test understanding of the instanceof check result, when the if block runs, and behavior if the check fails.