📖 Scenario: Imagine you are building a simple user profile system. You want to define a user interface that can be extended later with more details without changing the original interface.
🎯 Goal: Learn how to use declaration merging to extend interfaces in TypeScript by creating multiple interface declarations with the same name and combining their properties.
📋 What You'll Learn
Create an interface called User with two properties: name (string) and age (number).
Add a second declaration of the User interface with an additional property: email (string).
Create a variable user1 of type User with all three properties filled.
Print the user1 object to the console.
💡 Why This Matters
🌍 Real World
In real projects, you often need to extend existing interfaces from libraries or your own code without modifying the original interface. Declaration merging lets you do this cleanly.
💼 Career
Understanding declaration merging is useful for working with large TypeScript codebases, especially when integrating third-party libraries or adding features incrementally.
Progress0 / 4 steps
1
Create the initial User interface
Create an interface called User with two properties: name of type string and age of type number.
Typescript
Hint
Use the interface keyword followed by the name User. Inside curly braces, add name: string; and age: number;.
2
Add a second declaration of the User interface
Add another interface declaration called User with one property: email of type string.
Typescript
Hint
Write a new interface User block below the first one and add email: string; inside it.
3
Create a variable of type User
Create a variable called user1 of type User and assign it an object with name set to "Alice", age set to 30, and email set to "alice@example.com".
Typescript
Hint
Use const user1: User = { ... } and fill in the properties exactly as shown.
4
Print the user1 object
Write a console.log statement to print the user1 object.
Typescript
Hint
Use console.log(user1); to display the object in the console.
Practice
(1/5)
1. What happens when two interfaces with the same name are declared in TypeScript?
easy
A. Their properties merge into a single interface.
B. The second interface overwrites the first one.
C. A syntax error occurs due to duplicate names.
D. Only the first interface is used, others ignored.
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 A
Quick Check:
Declaration merging = properties combined [OK]
Hint: Same name interfaces combine properties automatically [OK]
Common Mistakes:
Thinking second interface overwrites the first
Expecting a syntax error on duplicate names
Assuming only the first interface is used
2. Which of the following is the correct syntax to merge two interfaces named Person in TypeScript?
easy
A. class Person { name: string } class Person { age: number }
B. interface Person { name: string } interface Person { age: number }
C. type Person = { name: string } type Person = { age: number }
D. interface Person { name: string } type Person = { age: number }
Solution
Step 1: Identify valid interface merging syntax
Only multiple interface declarations with the same name merge properties.
Step 2: Check each option
interface Person { name: string } interface Person { age: number } uses two interface Person declarations, 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 B
Quick Check:
Multiple interface declarations = merge [OK]
Hint: Only interfaces with same name merge, not types or classes [OK]
Common Mistakes:
Trying to merge types instead of interfaces
Using classes instead of interfaces
Mixing type and interface declarations
3. What will be the output of the following TypeScript code?
interface Car { brand: string; }
interface Car { year: number; }
const myCar: Car = { brand: "Toyota", year: 2020 };
console.log(myCar.year);
medium
A. undefined
B. Runtime error
C. Compilation error
D. 2020
Solution
Step 1: Understand interface merging effect
The two Car interfaces merge their properties, so Car has both brand and year.
Step 2: Analyze object and output
The object myCar correctly has both properties. Logging myCar.year outputs 2020.
Final Answer:
2020 -> Option D
Quick Check:
Interface merge allows all properties [OK]
Hint: Merged interfaces combine properties, so all exist in object [OK]
Common Mistakes:
Expecting a compilation error due to duplicate interface
Thinking only first interface properties exist
Confusing runtime and compile-time errors
4. Identify the error in the following TypeScript code that attempts interface merging:
interface User { id: number; }
interface User { name: string; }
const user: User = { id: 1 };
medium
A. Cannot assign number to 'id' property.
B. Duplicate interface declarations cause syntax error.
C. Missing property 'name' in object 'user'.
D. Interface merging does not work with 'const' declarations.
Solution
Step 1: Check interface merging and properties
The two User interfaces merge, so User requires both id and name.
Step 2: Analyze object assignment
The object user only has id, missing name, causing a type error.
Final Answer:
Missing property 'name' in object 'user'. -> Option C
Quick Check:
All merged properties required in object [OK]
Hint: Merged interfaces require all properties in objects [OK]