What does it mean to extend an interface in TypeScript?
Extending an interface means creating a new interface that inherits properties and methods from an existing interface, allowing reuse and addition of new members.
Click to reveal answer
intermediate
How do you extend multiple interfaces in TypeScript?
You list multiple interfaces separated by commas after the extends keyword, like interface NewInterface extends Interface1, Interface2 {}.
Click to reveal answer
intermediate
Can an interface extend a class in TypeScript?
Yes, an interface can extend a class, inheriting its members but without implementation, allowing the interface to describe the shape of the class.
Click to reveal answer
advanced
What happens if an extending interface redeclares a property from the base interface with a different type?
TypeScript will raise an error if the property types are incompatible, because the extending interface must be compatible with the base interface.
Click to reveal answer
beginner
Show a simple example of extending an interface with one additional property.
Which keyword is used to extend an interface in TypeScript?
Aimplements
Bextends
Cinherits
DextendsInterface
✗ Incorrect
The extends keyword is used to create a new interface that inherits from another interface.
Can an interface extend more than one interface?
ANo, only one interface can be extended
BYes, but only two interfaces
CNo, interfaces cannot extend other interfaces
DYes, by listing them separated by commas
✗ Incorrect
An interface can extend multiple interfaces by listing them separated by commas after the extends keyword.
What is the result if an extending interface changes the type of a property from the base interface incompatibly?
ATypeScript throws a type compatibility error
BTypeScript allows it without errors
CThe base interface is changed automatically
DThe property is ignored
✗ Incorrect
TypeScript enforces type compatibility and will throw an error if the property types conflict.
Which of the following is a valid way to extend interfaces?
Ainterface A extends B implements C {}
Binterface A implements B, C {}
Cinterface A extends B, C {}
Dinterface A inherits B, C {}
✗ Incorrect
Interfaces extend other interfaces using the extends keyword and can list multiple interfaces separated by commas.
Can an interface extend a class in TypeScript?
AYes, interfaces can extend classes
BNo, interfaces cannot extend classes
COnly abstract classes can be extended by interfaces
DOnly classes with no constructor can be extended
✗ Incorrect
Interfaces can extend classes to inherit their members' types without implementation.
Explain how extending interfaces helps in organizing and reusing code in TypeScript.
Think about how you can build on existing blueprints without rewriting everything.
You got /4 concepts.
Describe the syntax and rules for extending multiple interfaces in TypeScript.
Focus on how to list interfaces and what TypeScript expects.
You got /4 concepts.
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?