Array type annotation syntax in Typescript - Time & Space Complexity
When we write code with arrays, we often tell TypeScript what kind of items the array holds. This is called array type annotation.
We want to understand how the time to check or use these annotations grows as the array size grows.
Analyze the time complexity of the following code snippet.
function sumNumbers(numbers: number[]): number {
let total = 0;
for (const num of numbers) {
total += num;
}
return total;
}
This function sums all numbers in an array annotated as number[].
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element of the array.
- How many times: Once for every item in the array.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with the number of items.
Time Complexity: O(n)
This means the time to sum the array grows in a straight line as the array gets bigger.
[X] Wrong: "Type annotations slow down the program as the array grows because they add extra work at runtime."
[OK] Correct: Type annotations are only for the developer and compiler; they do not affect how fast the program runs when it is running.
Understanding how array operations scale helps you write clear and efficient code. Knowing that type annotations do not add runtime cost shows your grasp of TypeScript's design.
"What if we changed the array type from number[] to (number | string)[]? How would the time complexity change?"
Practice
Solution
Step 1: Understand array type syntax
In TypeScript, to declare an array of a specific type, you can usetype[]orArray<type>.Step 2: Check each option
let nums: number[];usesnumber[], which is correct.let nums: number;declares a single number, not an array.let nums: [number];declares a tuple with one number, not a general array.let nums: Array;is missing the type insideArray<>.Final Answer:
let nums: number[]; -> Option DQuick Check:
Array of numbers = number[] [OK]
type[] or Array<type> for arrays [OK]- Forgetting the brackets [] for arrays
- Using just the type without array syntax
- Confusing tuples with arrays
Solution
Step 1: Recall generic array syntax
The generic syntax for arrays in TypeScript isArray<type>, with angle brackets <>.Step 2: Evaluate options
let names: Array<string>;correctly usesArray<string>.let names: Array[string];uses square brackets instead of angle brackets, which is invalid.let names: string[];is valid syntax but uses the shorthand, not generic syntax.let names: Array();uses parentheses, which is incorrect.Final Answer:
let names: Array<string>; -> Option CQuick Check:
Generic array syntax = Array<type> [OK]
- Using square brackets [] instead of angle brackets <>
- Confusing shorthand and generic syntax
- Using parentheses instead of brackets
let fruits: string[] = ['apple', 'banana', 'cherry']; console.log(fruits[1]);
Solution
Step 1: Understand array indexing
Arrays in TypeScript (and JavaScript) start at index 0. So,fruits[0]is 'apple',fruits[1]is 'banana'.Step 2: Identify the logged value
The code logsfruits[1], which is 'banana'.Final Answer:
banana -> Option BQuick Check:
Index 1 in array = banana [OK]
- Thinking array indexes start at 1
- Confusing array elements with their indexes
- Expecting the whole array instead of one element
let scores: Array<number> = [10, 20, '30'];
Solution
Step 1: Check array type and elements
The array is declared asArray<number>, so all elements must be numbers.Step 2: Identify the wrong element
The element '30' is a string because of quotes, which conflicts with the number type.Step 3: Fix the error
Remove quotes from '30' to make it a number:30.Final Answer:
Remove quotes from '30' to make it a number -> Option AQuick Check:
Array type must match element types [OK]
- Mixing string and number types in typed arrays
- Ignoring type errors in array elements
- Changing array syntax instead of fixing element types
Solution
Step 1: Understand nested arrays
An array of arrays means each element is itself an array. So the type is an array of arrays of booleans.Step 2: Check each option
let matrix: boolean[][];uses shorthand nested arraysboolean[][], which is correct but not the generic syntax.let matrix: Array<boolean>;is a simple array of booleans, not nested.let matrix: Array;uses generic syntax for nested arrays:Array<Array<boolean>>, which is correct.let matrix: boolean[];is a simple array of booleans.Step 3: Choose the generic nested array syntax
Since the question asks for the correct type annotation (generic style),let matrix: Array;is the best fit.Final Answer:
let matrix: Array; -> Option AQuick Check:
Nested arrays = Array<Array<type>> [OK]
- Using single array type for nested arrays
- Confusing shorthand and generic syntax
- Missing one level of array nesting
Ready to prove it?
Pass the test and this pattern moves unknown
