Parameter type annotations in Typescript - Time & Space Complexity
Let's see how adding type annotations to function parameters affects the time it takes for the program to run.
We want to know if specifying types changes how long the code takes as input grows.
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;
}
This function adds up all numbers in an array, with a type annotation on the 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 input array.
As the array gets bigger, the function does more additions, one per item.
| 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 run grows in a straight line with the input size.
[X] Wrong: "Adding type annotations makes the code slower because it adds extra work at runtime."
[OK] Correct: Type annotations are only for the developer and compiler; they do not affect how many steps the program takes when running.
Understanding that type annotations help catch errors early but don't slow down your code shows you know the difference between coding tools and program speed.
"What if we changed the function to call itself recursively instead of using a loop? How would the time complexity change?"
Practice
What does a parameter type annotation in TypeScript do?
Solution
Step 1: Understand the purpose of parameter type annotations
Parameter type annotations specify the expected data type for each function parameter.Step 2: Identify what this means for the function
This helps the programmer and the TypeScript compiler catch errors if wrong data types are used.Final Answer:
It tells what kind of data a function expects for its parameters. -> Option AQuick Check:
Parameter type annotation = tells expected data type [OK]
- Thinking it changes the function's return type
- Believing it runs the function automatically
- Confusing it with removing parameters
Which of the following is the correct way to add a type annotation for a parameter named age that should be a number?
function checkAge(____) { return age > 18; }Solution
Step 1: Recall TypeScript parameter syntax
In TypeScript, the parameter name is followed by a colon and then the type.Step 2: Match the syntax to the options
Only age: number uses the correct syntax:age: number.Final Answer:
age: number -> Option BQuick Check:
Parameter name : type = correct syntax [OK]
- Putting type before parameter name
- Using equals sign instead of colon
- Using hyphen instead of colon
What will be the output of this TypeScript code?
function greet(name: string) { return `Hello, ${name}!`; }
console.log(greet(42));Solution
Step 1: Check the parameter type annotation
The function expects astringparameter namedname.Step 2: Analyze the argument passed
The argument passed is a number42, which does not match the expectedstringtype.Final Answer:
Type error at compile time -> Option AQuick Check:
Passing wrong type causes compile-time error [OK]
- Thinking it prints 'Hello, 42!' anyway
- Expecting a runtime error instead of compile-time
- Assuming undefined is printed
Identify the error in this TypeScript function and choose the fix:
function multiply(x: number, y) { return x * y; }Solution
Step 1: Check parameter annotations
Parameterxhas a typenumber, butyhas no type annotation.Step 2: Understand TypeScript's requirement
All parameters should have type annotations for clarity and type safety.Final Answer:
Add type annotation toy:y: number-> Option CQuick Check:
All parameters need type annotations [OK]
- Leaving some parameters untyped
- Changing unrelated parameter types
- Thinking return type annotation fixes parameter error
You want to write a function formatPrice that takes a price parameter which can be either a number or a string. How should you annotate the parameter to accept both types?
Solution
Step 1: Understand union types in TypeScript
To accept multiple types, use the union operator|between types.Step 2: Apply union type to parameter
Useprice: number | stringto allow either type.Final Answer:
price: number | string -> Option DQuick Check:
Use|for multiple allowed types [OK]
- Using & instead of | for union types
- Using comma-separated types
- Using any which loses type safety
Ready to prove it?
Pass the test and this pattern moves unknown
