Generic arrow functions in Typescript - Time & Space Complexity
We want to understand how the time it takes to run a generic arrow function changes as the input size grows.
Specifically, how does the function's work increase when given more data?
Analyze the time complexity of the following code snippet.
const identity = <T> (items: T[]): T[] => {
return items.map(item => item);
};
const numbers = [1, 2, 3, 4, 5];
const result = identity(numbers);
This code defines a generic arrow function that returns a new array by copying each item from the input array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
mapmethod loops over each item in the input array. - How many times: It runs once for every element in the array.
As the input array gets bigger, the function does more work by visiting each item once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size.
[X] Wrong: "Generic arrow functions always add extra hidden work and slow down the code a lot."
[OK] Correct: The generic part only helps with types and does not add extra loops or slow down the function itself.
Understanding how generic arrow functions behave helps you explain your code clearly and shows you know how input size affects performance.
What if the function used nested loops inside the map? How would the time complexity change?
Practice
What does the <T> represent in a generic arrow function in TypeScript?
Solution
Step 1: Understand generic type parameters
The<T>syntax declares a generic type parameter, which acts as a placeholder for any type.Step 2: Recognize its purpose in functions
This allows the function to work with different types while keeping type safety.Final Answer:
A placeholder for a type that will be specified later -> Option BQuick Check:
Generic type parameter = placeholder type [OK]
- Confusing <T> with a variable name
- Thinking <T> is a keyword like const or let
- Assuming <T> is an operator
Which of the following is the correct syntax for a generic arrow function that returns the same type it receives?
?Solution
Step 1: Check generic arrow function syntax
The generic type parameter<T>must come immediately after the function name without an equals sign.Step 2: Verify arrow function structure
The function takes an argument of typeTand returns the same typeT.const identity = <T>(arg: T): T => arg;matches this syntax correctly.Final Answer:
const identity = <T>(arg: T): T => arg; -> Option CQuick Check:
Correct generic arrow function syntax =const identity = <T>(arg: T): T => arg;[OK]
- Adding an equals sign after <T>
- Misplacing parentheses around <T>
- Using incorrect arrow function syntax
What will be the output of this TypeScript code?
const wrapInArray = <T>(value: T): T[] => [value];
console.log(wrapInArray(5));
console.log(wrapInArray('hello'));Solution
Step 1: Understand the generic function behavior
The functionwrapInArraytakes a value of any typeTand returns an array containing that value.Step 2: Analyze the console.log outputs
CallingwrapInArray(5)returns[5], andwrapInArray('hello')returns['hello'].Final Answer:
[5] ['hello'] -> Option AQuick Check:
Generic function returns array with input value [OK]
- Expecting output without array brackets
- Thinking it returns a tuple
- Confusing types causing errors
Identify the error in this generic arrow function and choose the corrected version:
const getFirst = <T> = (arr: T[]): T => {
return arr[0];
};Solution
Step 1: Identify incorrect syntax in original code
The original code incorrectly uses an equals sign after<T>, which is not valid syntax for generic arrow functions.Step 2: Choose the correct syntax
const getFirst = <T>(arr: T[]): T => arr[0];correctly places<T>immediately after the function name without an equals sign and uses concise arrow function syntax.Final Answer:
const getFirst = <T>(arr: T[]): T => arr[0]; -> Option DQuick Check:
Generic arrow function syntax =const getFirst = <T>(arr: T[]): T => arr[0];[OK]
- Adding equals sign after <T>
- Omitting generic parameter <T>
- Incorrect arrow function syntax
Given this generic arrow function, what is the output of mergeObjects({a: 1}, {b: 2})?
const mergeObjects = <T, U>(obj1: T, obj2: U): T & U => ({ ...obj1, ...obj2 });
console.log(mergeObjects({a: 1}, {b: 2}));Solution
Step 1: Understand the generic parameters and return type
The function takes two objects of typesTandUand returns their intersection typeT & U, meaning it combines properties from both.Step 2: Analyze the spread operator usage
The spread operator merges properties from both objects into a new object containing all keys and values.Step 3: Predict the output of the console.log
CallingmergeObjects({a: 1}, {b: 2})returns{ a: 1, b: 2 }.Final Answer:
{ a: 1, b: 2 } -> Option AQuick Check:
Spread merge of two objects = combined object [OK]
- Expecting only first or second object properties
- Confusing intersection type with union
- Thinking it causes runtime error
Ready to prove it?
Pass the test and this pattern moves unknown
