Recall & Review
beginner
What is declaration merging in TypeScript interfaces?
Declaration merging is when TypeScript combines multiple interface declarations with the same name into a single interface, merging their members together.
Click to reveal answer
beginner
Can you declare the same interface name multiple times in TypeScript?
Yes, TypeScript allows multiple declarations of the same interface name. It merges their properties into one interface.
Click to reveal answer
intermediate
What happens if two merged interfaces have properties with the same name but different types?
TypeScript will produce an error because the property types conflict and cannot be merged safely.
Click to reveal answer
intermediate
How does declaration merging help when working with third-party libraries?
It allows you to add new properties or methods to existing interfaces without modifying the original library code, enhancing flexibility.
Click to reveal answer
beginner
Show a simple example of declaration merging with interfaces in TypeScript.
Example:
interface User { name: string; }
interface User { age: number; }
// Merged interface User has both name and age properties.
Click to reveal answer
What does TypeScript do when it finds two interface declarations with the same name?
✗ Incorrect
TypeScript merges interface declarations with the same name into a single interface combining all members.
If two merged interfaces have a property with the same name but different types, what happens?
✗ Incorrect
TypeScript throws an error because conflicting property types cannot be merged.
Which of these is a benefit of declaration merging for interfaces?
✗ Incorrect
Declaration merging lets you extend interfaces safely without modifying original declarations.
Can declaration merging be used with classes in TypeScript?
✗ Incorrect
Declaration merging works with interfaces and namespaces, not with classes.
What will the merged interface contain?
interface A { x: number; }
interface A { y: string; }
✗ Incorrect
The merged interface A will have both properties x and y.
Explain declaration merging for interfaces in TypeScript and why it is useful.
Think about how you can add new properties without changing original code.
You got /5 concepts.
Describe what happens when two interfaces with the same name have conflicting property types during declaration merging.
Consider what happens if two friends disagree on the same thing.
You got /4 concepts.