Type erasure and its consequences in Typescript - Time & Space Complexity
When TypeScript code runs, its types disappear. This is called type erasure.
We want to see how this affects the speed of the program as it runs.
Analyze the time complexity of the following TypeScript function using generics.
function findFirst<T>(arr: T[], value: T): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
return i;
}
}
return -1;
}
This function looks for the first place a value appears in an array.
Look for loops or repeated checks.
- Primary operation: The for-loop checking each item one by one.
- How many times: Up to the length of the array, n times in the worst case.
As the array gets bigger, the function checks more items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the size of the array.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input gets bigger.
[X] Wrong: "Because TypeScript uses types, the program runs faster or slower depending on types."
[OK] Correct: Types are removed before running, so they do not affect speed or how many steps the program takes.
Understanding that TypeScript types vanish at runtime helps you explain why performance depends on the JavaScript code, not the types.
What if the function used a complex comparison instead of ===? How would that change the time complexity?
Practice
type erasure mean in TypeScript?Solution
Step 1: Understand TypeScript compilation
TypeScript compiles to JavaScript by removing all type annotations and interfaces.Step 2: Define type erasure meaning
This removal of type info is called type erasure, meaning types exist only during development, not at runtime.Final Answer:
TypeScript removes all type information when compiling to JavaScript. -> Option AQuick Check:
Type erasure = types removed at compile time [OK]
- Thinking types exist at runtime
- Believing TypeScript adds runtime type checks
- Confusing type erasure with type inference
Solution
Step 1: Recall type erasure effect
TypeScript types do not exist at runtime, so you cannot use them in runtime checks.Step 2: Identify runtime type check method
JavaScript'stypeofoperator works at runtime to check primitive types like string or number.Final Answer:
Usingtypeofoperator in JavaScript code. -> Option AQuick Check:
Runtime type check = typeof operator [OK]
- Trying to use TypeScript types at runtime
- Using interfaces for runtime checks
- Confusing instanceof with TypeScript types
function isString(value: any): boolean {
return typeof value === 'string';
}
console.log(isString(123));
console.log(isString('hello'));Solution
Step 1: Understand the function logic
The function returns true if the input's JavaScript type is 'string', false otherwise.Step 2: Evaluate the calls
For 123, typeof is 'number', so returns false. For 'hello', typeof is 'string', so returns true.Final Answer:
false\ntrue -> Option CQuick Check:
typeof 123 = number (false), typeof 'hello' = string (true) [OK]
- Assuming TypeScript types affect runtime
- Expecting true for number input
- Confusing output order
value is a number at runtime:function checkNumber(value: any): boolean {
if (value instanceof number) {
return true;
}
return false;
}Solution
Step 1: Understand instanceof usage
instanceofworks with objects, not primitive types like number.Step 2: Identify error cause
Usingvalue instanceof numbercauses a runtime error becausenumberis not a constructor function.Final Answer:
Usinginstanceofwith a primitive type causes a runtime error. -> Option DQuick Check:
instanceof with primitives = error [OK]
- Using instanceof with primitive types
- Thinking instanceof works like typeof
- Ignoring runtime errors from wrong instanceof usage
string | number and returns their length or value doubled?function process(input: string | number) {
// What is the best way to handle types here?
}Solution
Step 1: Recall type erasure impact
TypeScript types do not exist at runtime, so runtime checks must use JavaScript operators.Step 2: Choose correct runtime check
Usingtypeoflets you detect if input is 'string' or 'number' and handle each case safely.Step 3: Avoid incorrect methods
Relying only on TypeScript types or usinginstanceofwith primitives is unsafe or incorrect.Final Answer:
Use JavaScript runtime checks liketypeofto distinguish types and process accordingly. -> Option BQuick Check:
Runtime type safety = typeof checks [OK]
- Assuming TypeScript types exist at runtime
- Using instanceof with primitives
- Ignoring runtime type checks
Ready to prove it?
Pass the test and this pattern moves unknown
