TypeScript / TypeScript Interfaces and Types
Extending and Merging Interfaces
This tutorial will guide you through extending and merging interfaces in TypeScript, which allows us to create complex types based on existing ones.
Section overview
5 resourcesCovers defining and using interfaces and type aliases in TypeScript for object shapes and contracts.
1. Introduction
In this tutorial, we aim to deepen your understanding of TypeScript's interfaces, focusing on how they can be extended and merged to create complex and reusable types.
By the end of this guide, you will:
- Understand how to extend interfaces in TypeScript
- Learn how to merge interfaces in TypeScript
- Gain practical knowledge through code examples
Prerequisites: This tutorial assumes that you have a basic understanding of TypeScript interfaces. Knowledge of object-oriented programming would be beneficial but not necessary.
2. Step-by-Step Guide
Extending Interfaces
In TypeScript, an interface can extend from another interface, creating a new interface that contains all the members of the base interface and additional members defined in the extending interface. This is useful when you want to create a sub-type of an existing interface type.
interface Shape {
color: string;
}
interface Square extends Shape {
sideLength: number;
}
let square = <Square>{};
square.color = "blue";
square.sideLength = 10;
In the above example, Square extends from Shape, and hence it has two properties - color from Shape and its own sideLength.
Merging Interfaces
TypeScript allows us to merge the structures of two interfaces into one. When two interfaces with the same name are defined, TypeScript merges them into a single interface that contains all the members of both interfaces.
interface Box {
height: number;
width: number;
}
interface Box {
scale: number;
}
let box: Box = {height: 5, width: 6, scale: 0.5};
In the above example, two Box interfaces are defined. TypeScript merges them into a single Box interface that has three properties - height, width, and scale.
3. Code Examples
Extending Interfaces
// Defining the base interface
interface Vehicle {
name: string;
start(): void;
}
// Extending the base interface
interface Car extends Vehicle {
drive(): void;
}
// Implementing the extended interface
let myCar: Car = {
name: "Toyota",
start: function() {
console.log(this.name + " has started.");
},
drive: function() {
console.log(this.name + " is driving.");
}
};
myCar.start(); // Output: "Toyota has started."
myCar.drive(); // Output: "Toyota is driving."
Merging Interfaces
// First definition of the interface
interface Employee {
name: string;
}
// Second definition of the interface
interface Employee {
id: number;
}
// TypeScript merges the two definitions
let employee: Employee = {
name: "John Doe",
id: 123
};
console.log(employee); // Output: { name: 'John Doe', id: 123 }
4. Summary
In this tutorial, we've covered:
- How to extend an interface in TypeScript, which allows for creating a new interface that contains all the members of the base interface plus additional ones.
- How to merge interfaces in TypeScript, which results in a single interface that has all the members of the merged interfaces.
As next steps, you can explore other TypeScript features such as intersection types, type guards, and decorators. You can also delve into TypeScript's advanced types and utility types for more complex typing scenarios.
5. Practice Exercises
-
Exercise 1: Create a
Personinterface with propertiesnameandage. Extend it to create anEmployeeinterface with an additionalsalaryproperty. -
Exercise 2: Define two
Productinterfaces, one with anameproperty and another with apriceproperty. Merge them and create aProductobject.
Solutions:
- Solution 1:
interface Person {
name: string;
age: number;
}
interface Employee extends Person {
salary: number;
}
let employee: Employee = {
name: "John Doe",
age: 30,
salary: 50000
};
- Solution 2:
interface Product {
name: string;
}
interface Product {
price: number;
}
let product: Product = {
name: "Apple",
price: 1.5
};
Keep practicing with more complex scenarios and use cases to solidify your understanding of extending and merging interfaces in TypeScript.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article