Runtime type checking strategies in Typescript - Time & Space Complexity
When we check types while a program runs, it takes time. We want to know how this time grows as the data gets bigger.
How much longer does type checking take when we have more or bigger inputs?
Analyze the time complexity of the following code snippet.
function isStringArray(arr: unknown[]): boolean {
for (const item of arr) {
if (typeof item !== 'string') {
return false;
}
}
return true;
}
This code checks if every item in an array is a string by looking at each item one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the array.
- How many times: Once for each item until a non-string is found or all items are checked.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The time grows directly with the number of items because each item is checked once.
Time Complexity: O(n)
This means the time to check types grows in a straight line with the number of items.
[X] Wrong: "Type checking is instant and does not depend on input size."
[OK] Correct: Each item must be checked, so more items mean more work and more time.
Understanding how runtime checks grow with input size helps you explain your code's efficiency clearly and confidently.
"What if we checked nested arrays inside the main array? How would the time complexity change?"
Practice
value is a string at runtime in TypeScript?Solution
Step 1: Understand the typeof operator
Thetypeofoperator returns a string indicating the type of the operand. For strings, it returns 'string'.Step 2: Compare with 'string'
Checkingtypeof value === 'string'correctly verifies ifvalueis a string primitive.Final Answer:
Usetypeof value === 'string'-> Option CQuick Check:
typeof string = 'string' [OK]
- Using instanceof for string primitives
- Confusing Array.isArray with string check
- Comparing variable directly to String constructor
data is an array at runtime in TypeScript?Solution
Step 1: Recall the standard array check method
JavaScript and TypeScript provideArray.isArray()to check if a value is an array.Step 2: Verify the correct usage
CallingArray.isArray(data)returns true ifdatais an array, false otherwise.Final Answer:
Array.isArray(data) -> Option AQuick Check:
Array.isArray([]) = true [OK]
- Using typeof with 'array' (invalid)
- Using instanceof which can fail across frames
- Comparing constructor to string instead of function
function isNumber(value: unknown): boolean {
return typeof value === 'number';
}
console.log(isNumber('123'));
console.log(isNumber(123));Solution
Step 1: Check typeof for '123'
For the string '123',typeof '123'returns 'string', soisNumber('123')returns false.Step 2: Check typeof for 123
For the number 123,typeof 123returns 'number', soisNumber(123)returns true.Final Answer:
false true -> Option AQuick Check:
typeof '123' = string, typeof 123 = number [OK]
- Assuming string '123' is a number
- Confusing typeof results
- Ignoring return boolean values
function isStringArray(value: unknown): boolean {
return typeof value === 'string' && Array.isArray(value);
}Solution
Step 1: Analyze the current condition
The function checks ifvalueis both a string and an array, which is impossible.Step 2: Correct the logic for string arrays
We should check ifvalueis an array, then verify its elements are strings, e.g.,typeof value[0] === 'string'.Final Answer:
Check should betypeof value[0] === 'string'inside array -> Option DQuick Check:
Array with string elements needs element type check [OK]
- Checking typeof array itself as 'string'
- Ignoring element type checks
- Assuming array is string without Array.isArray
id. Which runtime type checking strategy is best?Solution
Step 1: Confirm data is an object
Usetypeof data === 'object'to ensuredatais an object (and not null).Step 2: Check the property type
Checktypeof data.id === 'number'to confirm theidproperty is numeric.Final Answer:
Checktypeof data === 'object'andtypeof data.id === 'number'-> Option BQuick Check:
Object with numeric id needs both checks [OK]
- Using Array.isArray for objects
- Checking string type for object
- Using instanceof Number for primitives
Ready to prove it?
Pass the test and this pattern moves unknown
