Extending interfaces in Typescript - Time & Space Complexity
When we extend interfaces in TypeScript, we combine properties from multiple interfaces into one. Understanding time complexity here means looking at how this combining process grows as we add more interfaces or properties.
We want to know: how does the work needed to merge interfaces change when interfaces get bigger or more numerous?
Analyze the time complexity of the following code snippet.
interface A {
x: number;
y: number;
}
interface B {
z: number;
}
interface C extends A, B {
w: number;
}
This code defines interfaces A and B, then creates interface C that extends both A and B, adding one more property.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Combining properties from each extended interface into one.
- How many times: Once per property in each interface being extended.
When we add more interfaces or more properties, the work to combine them grows with the total number of properties.
| Input Size (total properties) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of properties combined.
Time Complexity: O(n)
This means the time to combine interfaces grows linearly with the total number of properties involved.
[X] Wrong: "Extending multiple interfaces happens instantly no matter how many properties they have."
[OK] Correct: Each property must be processed to combine interfaces, so more properties mean more work.
Understanding how interface extension scales helps you reason about type system performance and design clean, maintainable code. This skill shows you think about how code grows, which is valuable in real projects.
"What if interface C extended three or more interfaces instead of two? How would the time complexity change?"