Default generic types in Typescript - Time & Space Complexity
We want to understand how the time it takes to run code with default generic types changes as the input grows.
How does using default types affect the work done when the code runs?
Analyze the time complexity of the following code snippet.
function wrapInArray<T = number>(item: T): T[] {
return [item];
}
const result1 = wrapInArray(5); // Uses default type number
const result2 = wrapInArray('hello'); // Uses string type explicitly
This code defines a function with a default generic type and calls it with different inputs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new array with one item.
- How many times: Once per function call.
Each call creates a single array with one element, so the work grows directly with how many times the function is called.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 array creations |
| 100 | 100 array creations |
| 1000 | 1000 array creations |
Pattern observation: The work grows in a straight line with the number of calls.
Time Complexity: O(n)
This means the time grows directly in proportion to how many times the function is called.
[X] Wrong: "Using default generic types makes the function slower or more complex."
[OK] Correct: The default generic type only sets a type when none is given; it does not add extra work when the function runs.
Understanding how default generic types affect performance helps you explain your code choices clearly and shows you know how types relate to runtime work.
"What if the function created an array of size n instead of just one item? How would the time complexity change?"
Practice
What is the main purpose of default generic types in TypeScript?
Solution
Step 1: Understand generic types
Generic types allow functions or classes to work with any type specified by the user.Step 2: Role of default generic types
Default generic types provide a fallback type if the user does not specify one explicitly.Final Answer:
To provide a fallback type when no type argument is specified -> Option AQuick Check:
Default generic types = fallback type [OK]
- Thinking default generics enforce a fixed type
- Confusing default generics with type conversion
- Believing default generics disable generics
Which of the following is the correct syntax to set a default generic type T to string in a function?
function example<T = string>()
Solution
Step 1: Recall TypeScript generic syntax
Default generic types use the equals sign (=) to assign a fallback type.Step 2: Analyze options
function example<T = string>() uses<T = string>which is correct syntax for default generic types.Final Answer:
function example<T = string>() -> Option CQuick Check:
Default generic syntax uses = [OK]
- Using colon (:) instead of equals (=)
- Adding question mark (?) incorrectly
- Confusing extends with default assignment
What will be the output of the following TypeScript code?
function wrap<T = number>(value: T): T[] {
return [value];
}
const result = wrap('hello');
console.log(result);Solution
Step 1: Understand default generic type usage
The functionwraphas a default generic typeT = number, but the caller passes a string 'hello', soTbecomesstring.Step 2: Analyze function output
The function returns an array containing the passed value, so it returns["hello"].Final Answer:
["hello"] -> Option BQuick Check:
Explicit type overrides default generic [OK]
- Assuming default generic always applies
- Expecting a type error for different type
- Confusing value with type name
Identify the error in this TypeScript code using default generic types:
function identity<T = number>(arg: T): T {
return arg.length;
}Solution
Step 1: Analyze the generic type and usage
The generic typeTdefaults tonumber, butnumberdoes not have alengthproperty.Step 2: Identify the error
Accessingarg.lengthcauses a TypeScript error becauselengthmay not exist on typeT.Final Answer:
Property 'length' may not exist on type 'T' -> Option AQuick Check:
Accessing unknown properties on generic types causes errors [OK]
- Ignoring property existence on generic types
- Thinking default generic restricts properties
- Assuming no error if default is number
Given this generic function with a default type, how can you call it to get an array of booleans?
function createArray<T = string>(length: number, value: T): T[] {
return Array(length).fill(value);
}Choose the correct call:
Solution
Step 1: Understand default generic and call signature
The function defaultsTtostring, but to get an array of booleans, we must specifyT = boolean.Step 2: Analyze options
createArray<boolean>(3, true) explicitly setsTtobooleanand passestrueas value, which matches the type.Final Answer:
createArray<boolean>(3, true) -> Option DQuick Check:
Specify generic type to override default [OK]
- Relying on default generic without specifying type
- Passing arguments in wrong order
- Mismatching generic type and value type
Ready to prove it?
Pass the test and this pattern moves unknown
