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?"
Practice
extends keyword do when used with interfaces in TypeScript?Solution
Step 1: Understand the role of
Theextendsin interfacesextendskeyword allows one interface to inherit properties from another, creating a new interface that includes all properties of the original.Step 2: Compare options with this understanding
Only It creates a new interface that inherits properties from an existing interface. correctly describes inheritance of properties. Other options describe unrelated or incorrect behaviors.Final Answer:
It creates a new interface that inherits properties from an existing interface. -> Option CQuick Check:
Extending interfaces means inheritance = D [OK]
- Thinking extends deletes properties
- Confusing interfaces with classes
- Believing extends merges unrelated interfaces
Person with a new interface Employee?Solution
Step 1: Recall TypeScript syntax for extending interfaces
The correct keyword to extend an interface isextends, used asinterface NewInterface extends ExistingInterface {}.Step 2: Check each option
interface Employee extends Person {} uses the correct syntax. Options B, C, and D use invalid keywords or symbols.Final Answer:
interface Employee extends Person {} -> Option AQuick Check:
Use 'extends' keyword for interface inheritance [OK]
- Using 'inherits' instead of 'extends'
- Using ':' like in classes
- Using symbols like '<' incorrectly
employee?
interface Person {
name: string;
}
interface Employee extends Person {
salary: number;
}
const employee: Employee = { name: "Alice", salary: 50000 };Solution
Step 1: Understand interface extension effect
TheEmployeeinterface extendsPerson, so it includes all properties ofPersonplus its own.Step 2: Analyze the assigned object
The object has bothname(string) andsalary(number), matching the combined interface.Final Answer:
{ name: string; salary: number; } -> Option AQuick Check:
Extended interface combines all properties = A [OK]
- Thinking only base interface properties apply
- Ignoring added properties in extension
- Expecting a type error incorrectly
interface Animal {
species: string;
}
interface Dog extends Animal {
breed: string;
}
const pet: Dog = { species: "Canine" };Solution
Step 1: Check properties required by extended interface
TheDoginterface requires bothspeciesandbreedproperties.Step 2: Verify object assigned to
The object only haspetspecies, missingbreed, causing a type error.Final Answer:
Missing property 'breed' in object assigned to 'pet'. -> Option DQuick Check:
All extended interface properties must be present [OK]
- Ignoring missing properties in assigned object
- Thinking extending interfaces causes syntax errors
- Confusing property types
interface Vehicle {
make: string;
model: string;
}
interface ElectricVehicle extends Vehicle {
batteryCapacity: number;
}How would you create a new interface
ElectricCar that extends ElectricVehicle and adds a range property (number)?Solution
Step 1: Understand interface extension chain
ElectricVehiclealready extendsVehicle, so extendingElectricVehicleincludes all properties from both.Step 2: Define
The correct syntax isElectricCarextendingElectricVehicleand addrangeinterface ElectricCar extends ElectricVehicle { range: number; }.Step 3: Evaluate other options
interface ElectricCar extends Vehicle ElectricVehicle { range: number; } has invalid syntax (missing comma). interface ElectricCar implements ElectricVehicle { range: number; } usesimplementswhich is invalid for interfaces. interface ElectricCar extends Vehicle { batteryCapacity: number; range: number; } misses extendingElectricVehicleand manually adds properties.Final Answer:
interface ElectricCar extends ElectricVehicle { range: number; } -> Option BQuick Check:
Extend the most specific interface and add new properties [OK]
- Trying to extend multiple interfaces unnecessarily
- Using 'implements' instead of 'extends' for interfaces
- Manually repeating inherited properties
Ready to prove it?
Pass the test and this pattern moves unknown
