What if you could combine many sets of rules into one with just a simple line of code?
Why Multiple interface extension in Typescript? - Purpose & Use Cases
Imagine you have several lists of rules for different roles in a team, like what a developer can do and what a designer can do. You try to write all these rules separately and then combine them by hand every time you create a new role.
This manual way is slow and confusing. You might forget some rules or repeat them many times. It's easy to make mistakes and hard to update the rules later because you have to change many places.
Multiple interface extension lets you create new roles by combining existing rule sets automatically. You write each set once, then extend them together to build new, complex roles without repeating code.
interface Developer { writeCode(): void; }
interface Designer { createDesign(): void; }
interface DevDesigner { writeCode(): void; createDesign(): void; }interface Developer { writeCode(): void; }
interface Designer { createDesign(): void; }
interface DevDesigner extends Developer, Designer {}You can build flexible and reusable role definitions easily by combining multiple interfaces into one.
In a project, you might have a "Manager" interface and a "TeamMember" interface. Using multiple interface extension, you can create a "TeamLead" interface that automatically has all the abilities of both without rewriting them.
Manual combination of interfaces is slow and error-prone.
Multiple interface extension lets you combine interfaces cleanly and reuse code.
This makes your code easier to maintain and extend.
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
