0
0
Typescriptprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - typeof type guards
Start with variable
Check typeof variable
Handle string
End
Check the type of a variable using typeof, then run code based on the type.
Execution Sample
Typescript
function printValue(x: string | number) {
  if (typeof x === 'string') {
    console.log('String:', x.toUpperCase());
  } else {
    console.log('Number:', x.toFixed(2));
  }
}
This code checks if x is a string or number and prints it differently.
Execution Table
StepVariable xConditionCondition ResultBranch TakenOutput
1'hello'typeof x === 'string'trueif branchString: HELLO
242typeof x === 'string'falseelse branchNumber: 42.00
3truetypeof x === 'string'falseelse branchNumber: Error (not handled)
💡 Execution stops after handling each input once.
Variable Tracker
Variable xStartAfter Step 1After Step 2After Step 3
xundefined'hello'42true
Key Moments - 2 Insights
Why does the code use typeof x === 'string' instead of x instanceof String?
typeof checks the primitive type directly, which works for string literals. instanceof checks object wrappers, which is less common and can cause confusion. See execution_table step 1 for typeof usage.
What happens if x is a boolean like true?
The else branch runs because typeof x === 'string' is false, but the code assumes number in else, causing an error. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when x is 'hello'?
AString: hello
BNumber: 42.00
CString: HELLO
DError
💡 Hint
Check row 1 in execution_table under Output column.
At which step does the condition typeof x === 'string' evaluate to false?
AStep 1
BStep 3
CStep 2
DNone
💡 Hint
Look at Condition Result column in execution_table for steps 2 and 3.
If we add a check for typeof x === 'boolean', what would change in the execution table?
AStep 1 would change output
BStep 3 would take a new branch for boolean
CStep 2 would become true
DNo change
💡 Hint
Refer to key_moments about boolean input and execution_table step 3.
Concept Snapshot
typeof type guards check a variable's type at runtime.
Syntax: if (typeof x === 'string') { ... } else { ... }
Allows safe use of type-specific methods.
Common types: 'string', 'number', 'boolean', 'undefined', 'object', 'function'.
Use to avoid errors when handling union types.
Full Transcript
This example shows how to use typeof type guards in TypeScript. The function printValue takes a variable x that can be a string or number. It checks if x is a string using typeof x === 'string'. If true, it prints the uppercase string. Otherwise, it treats x as a number and prints it with two decimals. The execution table shows three steps with different x values: 'hello', 42, and true. For 'hello', the condition is true and prints the uppercase string. For 42, the condition is false and prints the number. For true, the condition is false but the code assumes number, which can cause an error. The variable tracker shows how x changes each step. Key moments explain why typeof is used and what happens with unexpected types. The quiz tests understanding of outputs and condition results. The snapshot summarizes how typeof type guards work to safely handle different types in code.