Explicit type annotations in Typescript - Time & Space Complexity
We want to see how adding explicit type annotations affects the speed of running TypeScript code.
Does writing types change how fast the program runs?
Analyze the time complexity of the following code snippet.
function sumArray(numbers: number[]): number {
let total = 0;
for (const num of numbers) {
total += num;
}
return total;
}
const data = [1, 2, 3, 4, 5];
console.log(sumArray(data));
This code adds up all numbers in an array using explicit type annotations for the function and its parameter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the array.
- How many times: Once for every item in the array.
As the array gets bigger, the program does more additions, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input size grows.
[X] Wrong: "Adding explicit type annotations makes the program slower at running."
[OK] Correct: Type annotations help during coding but are removed when running, so they do not affect speed.
Understanding that type annotations help with code clarity but do not slow down the program shows you know how TypeScript works behind the scenes.
"What if we removed the explicit return type from the function? How would the time complexity change?"
Practice
explicit type annotations in TypeScript?Solution
Step 1: Understand what explicit type annotations do
Explicit type annotations specify the exact type for variables or functions in TypeScript.Step 2: Identify the purpose of explicit typing
This helps TypeScript catch mistakes and makes the code easier to understand.Final Answer:
To tell TypeScript exactly what type a variable or function uses -> Option AQuick Check:
Explicit type annotations = specify exact types [OK]
- Thinking explicit types speed up runtime
- Confusing type annotations with type conversion
- Believing explicit types disable type checking
Solution
Step 1: Recall TypeScript variable type annotation syntax
Type annotations use a colon after the variable name followed by the type.Step 2: Match the correct syntax
let name: string; useslet name: string;which is the correct way to declare a string type.Final Answer:
let name: string; -> Option BQuick Check:
Variable: type syntax = correct [OK]
- Using equals sign instead of colon for type
- Putting type inside quotes incorrectly
- Assigning type as a value instead of annotation
let age: number = 30; age = "thirty"; console.log(age);
Solution
Step 1: Check the variable type and assignment
The variableageis explicitly typed asnumber, but assigned a string "thirty" later.Step 2: Understand TypeScript's type checking
TypeScript will raise a compile-time error because the string assignment violates the number type annotation.Final Answer:
Type error at compile time -> Option AQuick Check:
Assigning wrong type = compile error [OK]
- Assuming code runs ignoring type mismatch
- Thinking TypeScript converts types automatically
- Expecting runtime error instead of compile error
function greet(name: string): void {
console.log("Hello, " + name);
}
greet(42);Solution
Step 1: Check function parameter type
The functiongreetexpects astringparameter namedname.Step 2: Check the function call argument
The callgreet(42)passes a number instead of a string, causing a type mismatch error.Final Answer:
Calling function with wrong argument type -> Option DQuick Check:
Argument type must match parameter type [OK]
- Ignoring parameter type annotations
- Assuming implicit type conversion
- Missing function return type is not an error here
function squareMap(nums: number[]): ??? {
const result = {};
for (const num of nums) {
result[num] = num * num;
}
return result;
}Solution
Step 1: Understand the function's return structure
The function returns an object mapping numbers to their squares, so keys and values are numbers.Step 2: Choose the best explicit return type
TypeScript'sRecord<number, number>type explicitly defines an object with number keys and number values, matching the function's output.Final Answer:
Record<number, number> -> Option CQuick Check:
Use Record for typed object maps [OK]
- Using string keys when keys are numbers
- Returning array instead of object
- Using index signature with wrong key type
Ready to prove it?
Pass the test and this pattern moves unknown
