Typeof operator in type context in Typescript - Time & Space Complexity
We want to understand how the time needed to check types using the typeof operator grows as the code runs.
Specifically, how does using typeof in a runtime context affect performance when the input size changes?
Analyze the time complexity of the following code snippet.
const data = [1, 'two', true, 4, 'five'];
function countStrings(arr: unknown[]): number {
let count = 0;
for (const item of arr) {
if (typeof item === 'string') {
count++;
}
}
return count;
}
countStrings(data);
This code counts how many string items are in an array using typeof in a type check.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the array and checking its type with
typeof. - How many times: Once for every item in the array (n times, where n is the array length).
Each new item adds one more type check, so the work grows steadily as the array grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 type checks |
| 100 | 100 type checks |
| 1000 | 1000 type checks |
Pattern observation: The number of operations grows directly in step with the input size.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input gets bigger.
[X] Wrong: "Using typeof in a runtime context is slow and adds extra loops."
[OK] Correct: The typeof check happens inside the existing loop and does not add extra loops or nested checks, so it does not increase the overall time complexity beyond the loop itself.
Understanding how simple type checks scale with input size helps you reason about performance in real code, showing you can think clearly about how your code behaves as data grows.
"What if we replaced the single loop with nested loops that check types inside each other? How would the time complexity change?"
Practice
typeof operator do when used in a type context in TypeScript?Solution
Step 1: Understand
In TypeScript,typeofin type contexttypeofused in a type context extracts the type of a variable or value, not its runtime value.Step 2: Differentiate from runtime
Unlike JavaScript's runtimetypeoftypeofthat returns a string, TypeScript'stypeofin type context returns the type itself.Final Answer:
It gets the type of a variable or value. -> Option BQuick Check:
typeofin type context = type extraction [OK]
typeof in types gets the variable's type [OK]- Confusing runtime
typeofwith type contexttypeof - Thinking it returns a string type
- Assuming it creates new variables
user using typeof in TypeScript?Solution
Step 1: Recall correct
The correct syntax istypeofsyntax in type contexttype NewType = typeof variableName;without parentheses.Step 2: Check each option
type UserType = typeof user; uses correct syntax. type UserType = type of user; has spaces and is invalid. type UserType = typeof(user); uses parentheses which is invalid in type context. type UserType = type(user); uses incorrect keyword.Final Answer:
type UserType = typeof user; -> Option CQuick Check:
Correct syntax =typeof variablewithout parentheses [OK]
typeof variable without parentheses for types [OK]- Adding parentheses after
typeof - Writing
type ofinstead oftypeof - Using runtime syntax in type context
const age = 30; type AgeType = typeof age; const myAge: AgeType = 25;
What is the type of
myAge?Solution
Step 1: Identify the type of
The variableageageis assigned the number 30, so its type isnumber.Step 2: Understand
This meansAgeType = typeof ageAgeTypeis the type ofage, which isnumber. SomyAgemust be a number.Final Answer:
number -> Option AQuick Check:
typeof age= number [OK]
- Confusing value with type
- Assuming
typeofreturns string - Using wrong type for
myAge
typeof in type context:const name = "Alice"; type NameType = typeof(name); const userName: NameType = "Bob";
Solution
Step 1: Check
In type context,typeofusage in type contexttypeofmust be used without parentheses. Using parentheses causes a syntax error.Step 2: Verify other parts
Variablenameis declared, assigning string is valid, and semicolons are optional in TypeScript.Final Answer:
Parentheses are not allowed aftertypeofin type context. -> Option AQuick Check:
No parentheses aftertypeofin types [OK]
typeof in type declarations [OK]- Adding parentheses after
typeof - Thinking semicolons are mandatory
- Confusing runtime and type syntax
configCopy? const config = {
url: "https://example.com",
timeout: 5000,
debug: true
};
type ConfigType = typeof config;
const configCopy: ConfigType = {
url: "https://test.com",
timeout: 3000,
debug: false
};Choose the best description of
configCopy's type.Solution
Step 1: Analyze the original
Theconfigobjectconfigobject has three properties:url(string),timeout(number), anddebug(boolean).Step 2: Understand
This creates a type matching the shape and types ofConfigType = typeof configconfig.Step 3: Check
configCopyassignmentconfigCopymust match the same property types asconfig, which it does.Final Answer:
An object with propertiesurl(string),timeout(number), anddebug(boolean). -> Option DQuick Check:
typeof config= object with matching properties [OK]
typeof to copy object shape and types exactly [OK]- Assuming
typeofreturns array or function - Ignoring property types in object
- Confusing object with array
Ready to prove it?
Pass the test and this pattern moves unknown
