TypeScript Intersection Types
Learn how to combine multiple types into one type using the intersection operator and build objects that satisfy all the required type definitions.
1. What is an Intersection Type?
TypeScript uses the ampersand operator (&) to create
an intersection type. It combines the requirements of two or more types.
For example, a person may have a name and age, while an employee has an employee ID and department. An intersection can combine these properties into a single Employee type.
Basic Syntax
type Person = {
name: string;
age: number;
};
type EmployeeInfo = {
employeeId: number;
department: string;
};
type Employee = Person & EmployeeInfo;
The Employee type now requires the properties from both
Person and EmployeeInfo.
2. Intersection Types Workflow Diagram
Follow the diagram to understand how separate type definitions combine into a single type.
Person Type
Defines name and age.
Employee Type
Defines employee ID and department.
Intersection
Combines the requirements of both types.
Combined Object
Contains every required property.
3. Basic Intersection Example
Let's combine person information and employee information into one object type.
Example
type Person = {
name: string;
age: number;
};
type EmployeeInfo = {
employeeId: number;
department: string;
};
type Employee = Person & EmployeeInfo;
const employee1: Employee = {
name: "Priya",
age: 25,
employeeId: 101,
department: "IT"
};
console.log(employee1.name);
console.log(employee1.age);
console.log(employee1.employeeId);
console.log(employee1.department);
Expected Output
Priya
25
101
IT
Understanding the Combined Object
Person
name: string
age: number
EmployeeInfo
employeeId: number
department: string
Employee
All Person properties
All EmployeeInfo properties
4. Combining More Than Two Types
An intersection can combine three or more types. This is useful when an object needs to satisfy multiple independent requirements.
Example: Product Information
type ProductDetails = {
name: string;
price: number;
};
type InventoryInfo = {
stock: number;
};
type CategoryInfo = {
category: string;
};
type Product = ProductDetails & InventoryInfo & CategoryInfo;
const product1: Product = {
name: "Keyboard",
price: 1200,
stock: 25,
category: "Electronics"
};
console.log(product1.name);
console.log(product1.price);
console.log(product1.stock);
console.log(product1.category);
Expected Output
Keyboard
1200
25
Electronics
Combination Diagram
ProductDetails
Name and price.
InventoryInfo
Stock quantity.
CategoryInfo
Product category.
Product
All combined properties.
5. Intersection Types with Interfaces
Intersection types can combine interfaces as well as type aliases. This makes it possible to compose several reusable object contracts.
Example
interface Person {
name: string;
}
interface Contact {
email: string;
phone: string;
}
type Customer = Person & Contact;
const customer1: Customer = {
name: "Rahul",
email: "rahul@example.com",
phone: "9876543210"
};
console.log(customer1.name);
console.log(customer1.email);
console.log(customer1.phone);
Expected Output
Rahul
rahul@example.com
9876543210
The Customer type combines the requirements of both interfaces, so the object must provide the name, email and phone properties.
6. Intersection of Function Types
Intersection types can also combine function types. The resulting type must satisfy both function signatures. This does not automatically combine two function implementations into a single callable function.
Example: A Value Supporting Two Operations
type TextOperation = (value: string) => string;
type NumberOperation = (value: number) => number;
type Operation = TextOperation & NumberOperation;
const operation: Operation = (value: string | number): string | number => {
if (typeof value === "string") {
return value.toUpperCase();
}
return value * 2;
};
console.log(operation("hello"));
console.log(operation(10));
Expected Output
HELLO
20
7. Intersection Types vs Union Types
Intersection and union types solve different problems. An intersection combines requirements, while a union permits a value to match one of several alternatives.
| Feature | Intersection (&) | Union (|) |
|---|---|---|
| Meaning | Must satisfy all combined type requirements. | Must match at least one of the listed types. |
| Operator | & |
| |
| Object example | Person & EmployeeInfo |
Student | Teacher |
| Typical use | Combining object capabilities or requirements. | Representing alternative values or structures. |
Visual Comparison
Intersection: A & B
A value must satisfy the requirements of A and B.
Union: A | B
A value can match A or B. Access to unique members may require type narrowing.
8. Important: Conflicting Properties
If two combined types declare the same property with incompatible types, the intersection may become difficult or impossible to satisfy. TypeScript does not simply choose one property's type.
Example
type StringId = {
id: string;
};
type NumberId = {
id: number;
};
type CombinedId = StringId & NumberId;
Here, id would need to be both a string and a number.
Those primitive types have no common assignable value, so a normal
object cannot satisfy this requirement.
9. Practical Example: Employee Management
In a real application, an employee object may need personal information, contact information and job information. Intersection types let us compose these definitions without repeating every property.
Example
type PersonalInfo = {
name: string;
age: number;
};
type ContactInfo = {
email: string;
};
type JobInfo = {
employeeId: number;
department: string;
};
type Employee = PersonalInfo & ContactInfo & JobInfo;
function displayEmployee(employee: Employee): void {
console.log("Name: " + employee.name);
console.log("Email: " + employee.email);
console.log("Employee ID: " + employee.employeeId);
console.log("Department: " + employee.department);
}
const employee1: Employee = {
name: "Neha",
age: 24,
email: "neha@example.com",
employeeId: 1001,
department: "Development"
};
displayEmployee(employee1);
Expected Output
Name: Neha
Email: neha@example.com
Employee ID: 1001
Department: Development
This approach keeps personal, contact and job-related definitions separate while letting the Employee type combine them.
10. Advantages of Intersection Types
Type Composition
Combine multiple reusable type definitions into one.
Less Duplication
Reuse shared object structures instead of repeating properties.
Clear Contracts
Make the required capabilities of an object explicit.
Maintainability
Keep reusable domain types organized and easier to update.
Summary
Intersection types use the & operator to combine type
requirements. They can combine type aliases, interfaces and other type
expressions. The resulting value must satisfy all the combined requirements.
Be careful when the combined types contain properties with conflicting types.
Next, learn Enums and how to define named constants for related values.