Type alias for functions in Typescript - Time & Space Complexity
We want to see how the time it takes to run code changes when we use type aliases for functions in TypeScript.
Specifically, how does the program's work grow as the input size grows?
Analyze the time complexity of the following code snippet.
type StringModifier = (input: string) => string;
function applyModifiers(input: string, modifiers: StringModifier[]): string {
let result = input;
for (const modify of modifiers) {
result = modify(result);
}
return result;
}
This code applies a list of string-changing functions one after another to an input string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of functions (modifiers).
- How many times: Once for each function in the modifiers array.
Each added function means one more step to apply to the string.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 function calls |
| 100 | 100 function calls |
| 1000 | 1000 function calls |
Pattern observation: The work grows directly with the number of functions to apply.
Time Complexity: O(n)
This means the time to finish grows in a straight line as you add more functions to apply.
[X] Wrong: "Using a type alias for functions makes the code slower because it adds extra steps."
[OK] Correct: The type alias only helps with naming and checking types; it does not add any extra work when running the code.
Understanding how loops over function lists affect time helps you explain how your code scales and shows you can think about performance clearly.
"What if each function in the modifiers array also loops over the string characters? How would the time complexity change?"
Practice
What is the main purpose of using a type alias for functions in TypeScript?
Solution
Step 1: Understand what a type alias does
A type alias gives a new name to a type, including function types, making code easier to read.Step 2: Identify the purpose for functions
For functions, it means naming the input and output types so you don't repeat them everywhere.Final Answer:
To give a simple name to a function's input and output types -> Option DQuick Check:
Type alias simplifies function types = A [OK]
- Thinking type alias runs the function
- Confusing type alias with classes
- Believing it improves runtime speed
Which of the following is the correct syntax to create a type alias MyFunc for a function that takes a string and returns a number?
type MyFunc = ?
Solution
Step 1: Recall TypeScript function type syntax
Function types use (parameters) => returnType format, with parameter names and types.Step 2: Match the correct syntax
(input: string) => number uses (input: string) => number, which is correct syntax for a function type alias.Final Answer:
(input: string) => number -> Option CQuick Check:
Function type alias syntax = D [OK]
- Omitting parameter name in type alias
- Using 'function' keyword inside type alias
- Writing parameter type without parentheses
Given the type alias and function below, what is the output of console.log(add('5', '10'))?
type AddFunc = (a: number, b: number) => number;
const add: AddFunc = (a, b) => a + b;
console.log(add('5', '10'));Solution
Step 1: Check the type alias and function parameters
The type alias expects two numbers as input and returns a number.Step 2: Analyze the function call with string arguments
Calling add('5', '10') passes strings, which conflicts with the expected number types, causing a compile-time error.Final Answer:
Type error at compile time -> Option AQuick Check:
Type mismatch causes compile error = C [OK]
- Assuming strings convert automatically to numbers
- Expecting runtime error instead of compile error
- Thinking output is string concatenation
Identify the error in this code using a type alias for a function:
type Greet = (name: string) => void;
const greet: Greet = (name) => {
return "Hello, " + name;
};Solution
Step 1: Check the type alias return type
The alias Greet specifies the function returns void, meaning no value returned.Step 2: Check the function implementation
The function returns a string "Hello, " + name, which conflicts with void return type.Final Answer:
The function should not return a string because return type is void -> Option AQuick Check:
Return type void means no return value = B [OK]
- Returning a value when return type is void
- Ignoring return type mismatch
- Confusing void with undefined
You want to create a type alias FilterFunc for a function that takes an array of numbers and a callback function. The callback takes a number and returns a boolean. The FilterFunc returns a new array of numbers filtered by the callback. Which is the correct type alias?
Solution
Step 1: Understand the function inputs
The function takes an array of numbers and a callback that returns boolean for each number.Step 2: Understand the function output
The function returns a new array of numbers filtered by the callback, so return type is number[].Step 3: Match the correct type alias
(arr: number[], callback: (num: number) => boolean) => number[] matches the input and output types correctly.Final Answer:
(arr: number[], callback: (num: number) => boolean) => number[] -> Option BQuick Check:
Filter function type = A [OK]
- Returning boolean[] instead of number[]
- Callback returning number instead of boolean
- Using void return type incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
