C extends both A and B, combining their properties.
Typescript
interface A {
a: string;
}
interface B {
b: string;
}
interface C extends A, B {
c: string;
}
Sample Program
This program shows how Dog extends Animal. The object myDog has all properties from both interfaces.
Typescript
interface Animal {
name: string;
age: number;
}
interface Dog extends Animal {
breed: string;
}
const myDog: Dog = {
name: "Buddy",
age: 5,
breed: "Golden Retriever"
};
console.log(`My dog's name is ${myDog.name}, age is ${myDog.age}, and breed is ${myDog.breed}.`);
OutputSuccess
Important Notes
You can extend multiple interfaces by separating them with commas.
If two interfaces have properties with the same name, they must have compatible types.
Extending interfaces helps keep your code DRY (Don't Repeat Yourself).
Summary
Extending interfaces lets you build new interfaces from existing ones.
Use extends keyword to inherit properties.
This makes your code cleaner and easier to maintain.
Practice
(1/5)
1. What does the extends keyword do when used with interfaces in TypeScript?
easy
A. It merges two unrelated interfaces without inheritance.
B. It deletes properties from an existing interface.
C. It creates a new interface that inherits properties from an existing interface.
D. It converts an interface into a class.
Solution
Step 1: Understand the role of extends in interfaces
The extends keyword 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 C
Quick Check:
Extending interfaces means inheritance = D [OK]
Hint: Remember: extends means inherit properties [OK]
Common Mistakes:
Thinking extends deletes properties
Confusing interfaces with classes
Believing extends merges unrelated interfaces
2. Which of the following is the correct syntax to extend an interface Person with a new interface Employee?
easy
A. interface Employee extends Person {}
B. interface Employee inherits Person {}
C. interface Employee : Person {}
D. interface Employee < Person {}
Solution
Step 1: Recall TypeScript syntax for extending interfaces
The correct keyword to extend an interface is extends, used as interface 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 A
Quick Check:
Use 'extends' keyword for interface inheritance [OK]
Hint: Use 'extends' keyword exactly for interfaces [OK]
Common Mistakes:
Using 'inherits' instead of 'extends'
Using ':' like in classes
Using symbols like '<' incorrectly
3. Given the code below, what is the type of employee?