Declaration merging for interfaces in Typescript - Time & Space Complexity
We want to understand how the time it takes to use merged interfaces changes as the interface size grows.
How does combining multiple interface declarations affect the work the program does?
Analyze the time complexity of the following code snippet.
interface User {
name: string;
}
interface User {
age: number;
}
function printUser(user: User) {
console.log(user.name, user.age);
}
This code merges two interface declarations into one combined interface used in a function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing properties of the merged interface object.
- How many times: Each property access happens once per call; no loops or recursion here.
As the interface grows with more merged declarations, the program accesses more properties when used.
| Input Size (number of properties) | Approx. Operations |
|---|---|
| 2 | 2 property accesses |
| 10 | 10 property accesses |
| 100 | 100 property accesses |
Pattern observation: The number of operations grows directly with the number of properties accessed.
Time Complexity: O(n)
This means the time to access all properties grows linearly with how many properties the merged interface has.
[X] Wrong: "Merging interfaces creates nested loops or extra hidden work that slows down the program a lot."
[OK] Correct: Merging just combines property lists; accessing them is straightforward and grows only with the number of properties, not loops.
Understanding how interface merging affects property access helps you reason about code structure and performance in real projects.
What if we added nested interfaces inside the merged interface? How would the time complexity change when accessing nested properties?
Practice
Solution
Step 1: Understand interface declaration merging
In TypeScript, interfaces with the same name combine their properties into one interface.Step 2: Effect of merging
This allows extending interfaces without overwriting or errors.Final Answer:
Their properties merge into a single interface. -> Option AQuick Check:
Declaration merging = properties combined [OK]
- Thinking second interface overwrites the first
- Expecting a syntax error on duplicate names
- Assuming only the first interface is used
Person in TypeScript?Solution
Step 1: Identify valid interface merging syntax
Only multipleinterfacedeclarations with the same name merge properties.Step 2: Check each option
interface Person { name: string } interface Person { age: number } uses twointerface Persondeclarations, which is correct. Options B and C mix types and interfaces or duplicate types, which do not merge. class Person { name: string } class Person { age: number } uses classes, which do not merge.Final Answer:
interface Person { name: string } interface Person { age: number } -> Option BQuick Check:
Multiple interface declarations = merge [OK]
- Trying to merge types instead of interfaces
- Using classes instead of interfaces
- Mixing type and interface declarations
interface Car { brand: string; }
interface Car { year: number; }
const myCar: Car = { brand: "Toyota", year: 2020 };
console.log(myCar.year);Solution
Step 1: Understand interface merging effect
The twoCarinterfaces merge their properties, soCarhas bothbrandandyear.Step 2: Analyze object and output
The objectmyCarcorrectly has both properties. LoggingmyCar.yearoutputs 2020.Final Answer:
2020 -> Option DQuick Check:
Interface merge allows all properties [OK]
- Expecting a compilation error due to duplicate interface
- Thinking only first interface properties exist
- Confusing runtime and compile-time errors
interface User { id: number; }
interface User { name: string; }
const user: User = { id: 1 };Solution
Step 1: Check interface merging and properties
The twoUserinterfaces merge, soUserrequires bothidandname.Step 2: Analyze object assignment
The objectuseronly hasid, missingname, causing a type error.Final Answer:
Missing property 'name' in object 'user'. -> Option CQuick Check:
All merged properties required in object [OK]
- Thinking duplicate interfaces cause syntax errors
- Ignoring missing properties in merged interface
- Assuming 'const' affects interface merging
interface Product { id: number; }
interface Product { description?: string; price: number; }Which of the following objects correctly implements the merged
Product interface?Solution
Step 1: Combine interface properties
The mergedProductinterface requiresid(number) andprice(number), with optionaldescription.Step 2: Check each object against required properties
{ id: 101, price: 29.99 } hasidandprice, valid. { id: 101, description: "Book" } missesprice. { price: 29.99, description: "Book" } missesid. { id: 101 } missesprice.Final Answer:
{ id: 101, price: 29.99 } -> Option AQuick Check:
Required properties must be present [OK]
- Ignoring required properties when optional ones exist
- Assuming optional properties are required
- Missing required properties in object
Ready to prove it?
Pass the test and this pattern moves unknown
