What if you could safely add new details to your interfaces anytime without breaking your code?
Why Declaration merging for interfaces in Typescript? - Purpose & Use Cases
Imagine you are building a big app and need to add new properties to an existing interface in different parts of your code. You try to rewrite the whole interface every time you want to add something.
This manual way is slow and risky. You might forget some properties or accidentally overwrite others. It becomes hard to keep track and maintain your code as it grows.
Declaration merging lets you split interface definitions across multiple places. TypeScript automatically combines them into one interface, so you can add properties safely and clearly without rewriting everything.
interface User { name: string; }
// Later
interface User { age: number; } // No error, merged automaticallyinterface User { name: string; }
interface User { age: number; } // Merged automaticallyIt enables you to extend and organize interfaces flexibly, making your code easier to grow and maintain.
When working with third-party libraries, you can add your own properties to their interfaces without changing the original code, keeping your customizations clean and safe.
Manual rewriting of interfaces is error-prone and hard to maintain.
Declaration merging lets TypeScript combine multiple interface parts automatically.
This makes extending interfaces easier and your code more organized.
Practice
Solution
Step 1: Understand interface declaration merging
In TypeScript, interfaces with the same name combine their properties into one interface.Step 2: Effect of merging
This allows extending interfaces without overwriting or errors.Final Answer:
Their properties merge into a single interface. -> Option AQuick Check:
Declaration merging = properties combined [OK]
- Thinking second interface overwrites the first
- Expecting a syntax error on duplicate names
- Assuming only the first interface is used
Person in TypeScript?Solution
Step 1: Identify valid interface merging syntax
Only multipleinterfacedeclarations with the same name merge properties.Step 2: Check each option
interface Person { name: string } interface Person { age: number } uses twointerface Persondeclarations, which is correct. Options B and C mix types and interfaces or duplicate types, which do not merge. class Person { name: string } class Person { age: number } uses classes, which do not merge.Final Answer:
interface Person { name: string } interface Person { age: number } -> Option BQuick Check:
Multiple interface declarations = merge [OK]
- Trying to merge types instead of interfaces
- Using classes instead of interfaces
- Mixing type and interface declarations
interface Car { brand: string; }
interface Car { year: number; }
const myCar: Car = { brand: "Toyota", year: 2020 };
console.log(myCar.year);Solution
Step 1: Understand interface merging effect
The twoCarinterfaces merge their properties, soCarhas bothbrandandyear.Step 2: Analyze object and output
The objectmyCarcorrectly has both properties. LoggingmyCar.yearoutputs 2020.Final Answer:
2020 -> Option DQuick Check:
Interface merge allows all properties [OK]
- Expecting a compilation error due to duplicate interface
- Thinking only first interface properties exist
- Confusing runtime and compile-time errors
interface User { id: number; }
interface User { name: string; }
const user: User = { id: 1 };Solution
Step 1: Check interface merging and properties
The twoUserinterfaces merge, soUserrequires bothidandname.Step 2: Analyze object assignment
The objectuseronly hasid, missingname, causing a type error.Final Answer:
Missing property 'name' in object 'user'. -> Option CQuick Check:
All merged properties required in object [OK]
- Thinking duplicate interfaces cause syntax errors
- Ignoring missing properties in merged interface
- Assuming 'const' affects interface merging
interface Product { id: number; }
interface Product { description?: string; price: number; }Which of the following objects correctly implements the merged
Product interface?Solution
Step 1: Combine interface properties
The mergedProductinterface requiresid(number) andprice(number), with optionaldescription.Step 2: Check each object against required properties
{ id: 101, price: 29.99 } hasidandprice, valid. { id: 101, description: "Book" } missesprice. { price: 29.99, description: "Book" } missesid. { id: 101 } missesprice.Final Answer:
{ id: 101, price: 29.99 } -> Option AQuick Check:
Required properties must be present [OK]
- Ignoring required properties when optional ones exist
- Assuming optional properties are required
- Missing required properties in object
Ready to prove it?
Pass the test and this pattern moves unknown
