Declaration merging lets you combine multiple interface definitions with the same name into one. This helps organize and extend code easily.
Declaration merging for interfaces in Typescript
interface InterfaceName {
property1: Type1;
}
interface InterfaceName {
property2: Type2;
}All interface declarations with the same name merge into one combined interface.
If properties have the same name, their types must be compatible or an error occurs.
name and age properties.interface User {
name: string;
}
interface User {
age: number;
}Config interface with debug and version.interface Config {
debug: boolean;
}
interface Config {
version: string;
}This program shows how two interface declarations with the same name merge. The Person interface has both firstName and lastName. The object user uses both properties.
interface Person {
firstName: string;
}
interface Person {
lastName: string;
}
const user: Person = {
firstName: "Alice",
lastName: "Smith"
};
console.log(`Full name: ${user.firstName} ${user.lastName}`);Declaration merging only works with interfaces, not with types or classes.
Be careful to avoid property name conflicts with incompatible types.
Interfaces with the same name merge their properties into one.
This helps extend or organize interfaces across code.
Use declaration merging to add properties without changing original interface code.
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
