Equality narrowing in Typescript - Time & Space Complexity
We want to understand how the time it takes to run code changes when we use equality narrowing in TypeScript.
Specifically, we ask: how does checking equality affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
function processValue(x: string | number) {
if (x === 42) {
return "The answer";
} else if (x === "hello") {
return "Greeting";
} else {
return "Unknown";
}
}
This code checks if the input matches specific values and returns a result based on that.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A few equality checks (comparisons) done one after another.
- How many times: Each check happens once per function call, no loops or repeated traversals.
Since the code only does a fixed number of checks, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 checks |
| 100 | 2 checks |
| 1000 | 2 checks |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the code stays constant no matter the input size.
[X] Wrong: "More input values mean more time because the checks will repeat for each possible value."
[OK] Correct: The code only checks a fixed number of values once, so input size does not affect the number of checks.
Understanding how simple equality checks work helps you explain how your code runs efficiently and why some checks don't slow down your program as input grows.
"What if we added a loop to check the input against many values? How would the time complexity change?"
Practice
equality narrowing in TypeScript help you do?Solution
Step 1: Understand equality narrowing concept
Equality narrowing means TypeScript uses equality checks (like ===) to know the exact type of a variable.Step 2: Compare options with definition
Only It lets TypeScript know the exact type after checking equality. correctly describes this behavior. Others are incorrect or unrelated.Final Answer:
It lets TypeScript know the exact type after checking equality. -> Option AQuick Check:
Equality narrowing = exact type after equality check [OK]
- Thinking it converts types automatically
- Believing it disables type checking
- Assuming it only works for numbers
Solution
Step 1: Identify correct equality operator for narrowing
TypeScript uses strict equality===for safe type narrowing.Step 2: Check each option
if (value === 10) { } uses===, which is correct. if (value == 10) { } uses loose equality, which is less safe. if (value = 10) { } is assignment, not comparison. if (value != 10) { } is inequality, not equality.Final Answer:
if (value === 10) { } -> Option AQuick Check:
Use === for equality narrowing [OK]
- Using = instead of ===
- Using == which is not strict
- Confusing != with ===
function check(x: string | number) {
if (typeof x === 'string') {
return x.toUpperCase();
} else {
return x.toFixed(2);
}
}
console.log(check(3.14159));Solution
Step 1: Understand type narrowing in function
The function checks ifxis a string or number usingtypeof. For number, it callstoFixed(2).Step 2: Evaluate output for input 3.14159
Since input is number, it returns3.14159.toFixed(2)which is string "3.14".Final Answer:
"3.14" -> Option CQuick Check:
Number input returns toFixed string "3.14" [OK]
- Thinking toFixed returns number
- Confusing string and number output
- Ignoring type narrowing with typeof
function process(value: string | number) {
if (value === 'hello') {
return value.toUpperCase();
} else {
return value.toUpperCase();
}
}Solution
Step 1: Analyze type narrowing with equality check
The checkvalue === 'hello'narrowsvalueto string only if it equals 'hello'. Else block still hasvalueas string or number.Step 2: Identify error in else block
CallingtoUpperCase()onvaluein else block is unsafe becausevaluecould be number, causing runtime error.Final Answer:
Error: calling toUpperCase on number in else block. -> Option DQuick Check:
toUpperCase on number causes error [OK]
- Assuming else block is string only
- Using == instead of === for narrowing
- Ignoring runtime errors from wrong method calls
function formatValue(val: string | number | null) {
if (val === null) {
return "No value";
} else if (typeof val === 'string') {
return val.trim();
} else {
return val.toFixed(1);
}
}Solution
Step 1: Check null handling
The first if checksval === null, safely handling null case.Step 2: Check type narrowing for string and number
The else if usestypeof val === 'string'to narrow string, else block handles number withtoFixed(1).Step 3: Confirm all cases covered
All possible types (null, string, number) are handled safely with proper narrowing.Final Answer:
Code is correct and safely narrows all types. -> Option BQuick Check:
All types handled safely with === and typeof [OK]
- Forgetting null check
- Using == null which includes undefined
- Calling toFixed without type check
Ready to prove it?
Pass the test and this pattern moves unknown
