Multiple interface extension in Typescript - Time & Space Complexity
We want to understand how the time it takes to run code changes when using multiple interface extensions in TypeScript.
Specifically, how does combining interfaces affect the work the program does?
Analyze the time complexity of the following code snippet.
interface A {
methodA(): void;
}
interface B {
methodB(): void;
}
interface C extends A, B {
methodC(): void;
}
function useC(obj: C) {
obj.methodA();
obj.methodB();
obj.methodC();
}
This code shows an interface C that extends two interfaces A and B, combining their methods. Then a function calls all methods from C.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling three methods on the object.
- How many times: Exactly three calls, no loops or recursion.
Since the number of method calls is fixed, the work does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 |
| 100 | 3 |
| 1000 | 3 |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the code stays constant regardless of input size.
[X] Wrong: "Extending multiple interfaces makes the code slower as input grows."
[OK] Correct: The interface extension only combines method types; it does not add loops or repeated work, so execution time stays constant.
Understanding how interface extension affects code helps you explain design choices clearly and shows you know how TypeScript structures work without adding hidden costs.
"What if the function useC called the methods inside a loop over an array of objects? How would the time complexity change?"
Practice
interface C extends A, B mean in TypeScript?Solution
Step 1: Understand interface extension
When an interface extends others, it inherits all their properties.Step 2: Apply to multiple interfaces
Extending both A and B means C has all properties from both interfaces combined.Final Answer:
Interface C includes all properties from interfaces A and B. -> Option DQuick Check:
Multiple interface extension = combined properties [OK]
- Thinking extension replaces interfaces
- Believing only one interface's properties are included
- Confusing extension with implementation
A and B in TypeScript?Solution
Step 1: Recall TypeScript interface extension syntax
Interfaces extend others usingextendsfollowed by comma-separated interfaces.Step 2: Check each option
interface C extends A, B {} uses correct syntax:interface C extends A, B {}. Others are invalid syntax.Final Answer:
interface C extends A, B {} -> Option AQuick Check:
Correct syntax uses extends with commas [OK]
- Using implements instead of extends for interfaces
- Using parentheses or & inside extends
- Using 'inherits' keyword which doesn't exist
interface A { x: number; }
interface B { y: string; }
interface C extends A, B { z: boolean; }
const obj: C = { x: 10, y: "hello", z: true };What is the type of
obj?Solution
Step 1: Understand interface C extension
Interface C extends A and B, so it has properties x (number), y (string), and its own z (boolean).Step 2: Analyze object type
The object has all three properties with correct types, matching combined interface C.Final Answer:
{ x: number; y: string; z: boolean; } -> Option BQuick Check:
Extended interface properties combine in object type [OK]
- Ignoring properties from one of the extended interfaces
- Missing the new property z in interface C
- Confusing property types
interface A { a: number; }
interface B { b: string; }
interface C extends A, B {}
const obj: C = { a: 5 };Solution
Step 1: Check interface C properties
Interface C extends A and B, so it requires properties 'a' (number) and 'b' (string).Step 2: Check object assigned to C
The object only has 'a', missing required 'b' property, causing a type error.Final Answer:
Property 'b' is missing in object assigned to type C. -> Option AQuick Check:
All extended interface properties must be present [OK]
- Assuming extending multiple interfaces is invalid
- Ignoring missing properties in object
- Confusing property types
interface Name { first: string; last: string; }
interface Contact { email: string; phone: string; }
interface Employee extends Name, Contact { id: number; }Which object correctly implements
Employee?Solution
Step 1: List required properties of Employee
Employee extends Name and Contact, so it needs first, last, email, phone, and also id.Step 2: Check each object for all required properties
Only { first: "Jane", last: "Doe", email: "jane@example.com", phone: "123-456", id: 101 } has all five properties with correct types.Final Answer:
{ first: "Jane", last: "Doe", email: "jane@example.com", phone: "123-456", id: 101 } -> Option CQuick Check:
All extended interface properties plus own must be present [OK]
- Missing some properties from extended interfaces
- Confusing which properties belong to which interface
- Leaving out the new property id
Ready to prove it?
Pass the test and this pattern moves unknown
