Recall & Review
beginner
What does it mean to merge a class with an interface in TypeScript?It means adding extra type information or members to a class by declaring an interface with the same name. The class and interface combine, so the class must satisfy both its own members and the interface's members.Click to reveal answer
beginner
How do you declare an interface to merge with an existing class named <code>Person</code>?You declare an interface with the exact same name: <br><pre>interface Person { <br> newProperty: string; <br>}</pre> This adds <code>newProperty</code> to the <code>Person</code> class instance type.Click to reveal answer
intermediate
Can merged interfaces add properties to the class's instance type or static type?
Merged interfaces add properties to the class's instance type, not the static type. To add static members, you must merge with the class's constructor type separately.
Click to reveal answer
intermediate
What happens if the class does not implement the members declared in the merged interface?TypeScript will show a type error because the class instance does not satisfy the interface's contract. You must implement all interface members in the class or provide them via other means.Click to reveal answer
beginner
Why is merging classes with interfaces useful in TypeScript?
It allows you to extend or add type information to existing classes without modifying their original code. This is helpful for adding properties or methods for typing purposes, especially with third-party libraries.
Click to reveal answer
What must an interface have to merge correctly with a class in TypeScript?
✗ Incorrect
Interfaces merge with classes only if they share the exact same name.
When merging an interface with a class, what type of members does the interface add?
✗ Incorrect
Merged interfaces add instance members to the class type.
If a class
Car is merged with an interface adding a property color, what must the class do?✗ Incorrect
The class must implement all properties declared in the merged interface.
Can merging interfaces add static members to a class type directly?
✗ Incorrect
Static members require merging with the class constructor type, not the instance interface.
Why might you merge an interface with a class from a third-party library?
✗ Incorrect
Merging interfaces lets you extend types safely without modifying external code.
Explain how merging a class with an interface works in TypeScript and why it is useful.
Think about how interfaces and classes combine by name and what that means for the class type.
You got /4 concepts.
Describe the difference between adding instance members and static members when merging interfaces with classes.
Consider what part of the class the interface affects.
You got /3 concepts.