Introduction to Intersection Types
TypeScript provides several ways to combine types. Union types allow a value to satisfy one of several alternatives, while intersection types combine multiple requirements into a single type.
An intersection type uses the ampersand symbol (&) between two or more types. For example, User & Employee describes a value that must satisfy both the User type and the Employee type.
Intersections are especially useful when an object needs properties from multiple contracts. You can define reusable types for common properties, combine them when needed, and avoid repeating the same property declarations in multiple places.
Intersection types can also combine function types, primitive types, and other type expressions. However, intersections work best when their requirements are compatible. If two types require the same property to have incompatible types, the resulting type can become impossible to satisfy.
1. Creating a Basic Intersection Type
The simplest intersection combines two object types. The resulting type contains the requirements of both original types, so a compatible value must provide all required properties.
type User = {
id: number;
name: string;
};
type Employee = {
department: string;
salary: number;
};
type EmployeeUser = User & Employee;
const employee: EmployeeUser = {
id: 101,
name: "Asha",
department: "Engineering",
salary: 75000
};
console.log(employee.name);
console.log(employee.department);
console.log(employee.salary);
User defines id and name, while Employee defines department and salary. The EmployeeUser intersection requires all four properties.
An object that contains only the properties from User would not satisfy EmployeeUser because it would be missing department and salary.
This pattern is useful when several concepts need to be combined into a larger model without duplicating their declarations. Each smaller type describes one responsibility, and the intersection composes those responsibilities into a complete contract.
2. Combining Multiple Types
An intersection can combine more than two types. You can use it to construct an object that satisfies several independent contracts simultaneously.
type Identifiable = {
id: number;
};
type Named = {
name: string;
};
type Timestamped = {
createdAt: Date;
};
type Course = Identifiable & Named & Timestamped;
const course: Course = {
id: 201,
name: "TypeScript Fundamentals",
createdAt: new Date()
};
console.log(course.id);
console.log(course.name);
console.log(course.createdAt);
Identifiable requires an id, Named requires a name, and Timestamped requires a createdAt value. The Course type combines all three requirements.
You can reuse these smaller types in other models. For example, Product and Article could both include Identifiable and Timestamped while maintaining their own specific properties.
Intersection composition is particularly useful in larger applications where different entities share capabilities or metadata but do not need to be described by one large, repetitive type.
3. Intersection Types with Interfaces
Intersection types work with interfaces as well as type aliases. You can combine interface types using the ampersand operator without changing how their properties are checked.
interface BasicInfo {
name: string;
email: string;
}
interface AccountInfo {
userId: number;
active: boolean;
}
type Account = BasicInfo & AccountInfo;
const account: Account = {
name: "Asha",
email: "asha@example.com",
userId: 101,
active: true
};
console.log(account.name);
console.log(account.userId);
console.log(account.active);
BasicInfo and AccountInfo remain separate interfaces. The Account type composes their requirements into one type that needs every required member.
Intersections are also different from interface inheritance. An interface can extend other interfaces when their declarations are compatible, while an intersection creates a type expression that combines existing types.
interface HasId {
id: number;
}
interface HasName {
name: string;
}
interface User extends HasId, HasName {
email: string;
}
type Product = HasId & HasName & {
price: number;
};
Both approaches can combine compatible object contracts. Use interface extends when you are defining a new interface based on existing interfaces. Use intersection types when composing types through aliases, combining type expressions, or adding properties to an existing type.
4. Adding Properties with Intersection Types
One practical pattern is to combine an existing type with a new object type. This lets you extend the required structure without editing the original declaration.
type User = {
id: number;
name: string;
};
type UserWithRole = User & {
role: string;
};
const user: UserWithRole = {
id: 101,
name: "Asha",
role: "Administrator"
};
console.log(user.name);
console.log(user.role);
UserWithRole preserves the requirements of User and adds a required role property. The original User type remains unchanged, so other parts of the application can continue using the smaller contract.
This is useful when you need a specialized version of an existing model. For instance, an ordinary user might have id and name, while an administrative screen needs those properties plus role and lastLoginAt.
Remember that the added properties are required unless you explicitly mark them as optional. Intersecting a type with an object type does not automatically make the new properties optional.
5. Intersection Types and Shared Properties
When multiple intersected types declare the same property, the property must satisfy the requirements from every declaration. If both types use compatible property types, the shared property can be used normally.
type HasName = {
name: string;
};
type HasDisplayName = {
name: string;
displayName: string;
};
type Person = HasName & HasDisplayName;
const person: Person = {
name: "Asha",
displayName: "Asha S."
};
console.log(person.name);
console.log(person.displayName);
Both types require name to be a string. The shared requirement is compatible, so the intersection can be satisfied by an object that provides the name and displayName properties.
The situation changes when the property types conflict. Consider a type that requires age to be a number and another that requires age to be a string.
type NumericAge = {
age: number;
};
type TextAge = {
age: string;
};
type ConflictingPerson = NumericAge & TextAge;
// No ordinary value can satisfy both requirements:
// const person: ConflictingPerson = {
// age: 25
// };
The resulting age property must satisfy number and string simultaneously. Since no ordinary value is both a number and a string, this intersection cannot be populated with a normal age value.
Before combining types, check whether shared property names represent the same concept and use compatible types. When the requirements conflict, redesign the model or use a different type composition strategy rather than expecting the intersection to choose one property definition.
6. Intersections with Incompatible Types and never
The never type represents values that cannot occur. Intersecting completely incompatible types can produce never because no value can satisfy all the requirements.
type Impossible = string & number;
// Impossible is effectively never.
type Valid = string & {};
const message: Valid = "Hello";
console.log(message);
A value cannot be a primitive string and a primitive number at the same time, so string & number is reduced to never.
Intersections are not limited to object types. They can be used with primitives and other type expressions, but many combinations are not useful because their requirements cannot be satisfied.
The example string & {} remains compatible with string under TypeScript's type rules for non-nullish values. However, be careful with conclusions about never when object properties conflict: a conflicting property can make that property itself never without necessarily reducing every surrounding object type to never.
Understanding this distinction helps when debugging complex intersections. Inspect the conflicting member types rather than assuming that every unsatisfiable property automatically makes the entire object type display as never in every context.
7. Intersection Types in Function Parameters
An intersection can define a function parameter that needs several capabilities. This allows the function to access every property guaranteed by the combined type.
type HasName = {
name: string;
};
type HasEmail = {
email: string;
};
function sendWelcomeEmail(
user: HasName & HasEmail
): string {
return "Welcome " + user.name +
". Email sent to " + user.email;
}
const account = {
id: 101,
name: "Asha",
email: "asha@example.com"
};
console.log(sendWelcomeEmail(account));
The function requires both name and email. Other properties, such as id, can exist on the supplied value but are not required by the parameter type.
This style is useful when a function needs multiple capabilities without depending on one large application-specific interface. The function can accept any structurally compatible object that provides every required member.
Do not combine unnecessary requirements into a function parameter. A function that only reads a name should generally require HasName rather than HasName & HasEmail, since the narrower contract makes the function easier to reuse.
8. Intersection Types with Generics
Generics let you create reusable functions that preserve a specific input type. An intersection in a generic return type can express that the returned object retains the original properties and also includes an additional set of properties.
function addTimestamp<T extends object>(
item: T
): T & { createdAt: Date } {
return {
...item,
createdAt: new Date()
};
}
const user = {
id: 101,
name: "Asha"
};
const result = addTimestamp(user);
console.log(result.id);
console.log(result.name);
console.log(result.createdAt);
The generic parameter T represents the specific input object type. The return type combines T with an additional object containing createdAt.
As a result, TypeScript knows that result retains the original id and name properties and also has createdAt. This makes the helper reusable for many object types.
The object spread copies enumerable own properties from the input into a new object, after which createdAt is added. This example assumes that the additional property does not create an incompatible existing property in the input's declared type. If inputs may already contain createdAt with another type, define a clearer contract or constrain and validate the input appropriately.
Type annotations alone cannot guarantee runtime behavior for untrusted input. Keep the implementation aligned with its declared return type, especially when data comes from APIs, dynamically typed code, or external JavaScript libraries.
9. Intersection Types and Branded Types
TypeScript's structural typing can make two domain values interchangeable when their structures are identical. For example, a user identifier and an order identifier represented as numbers may be accepted wherever a number is expected.
A branded type pattern can introduce a distinguishing property into a type so that otherwise similar values are less likely to be confused. Intersection types are commonly used to combine a primitive type with this additional marker.
declare const userIdBrand: unique symbol;
declare const orderIdBrand: unique symbol;
type UserId = number & {
readonly [userIdBrand]: "UserId";
};
type OrderId = number & {
readonly [orderIdBrand]: "OrderId";
};
function getUserName(id: UserId): string {
return "User " + id;
}
UserId and OrderId are both based on numbers, but their additional unique-symbol properties distinguish them during type checking. A normal number does not automatically satisfy either branded type.
The brand declarations above are type-level markers. They do not add actual properties to numeric values at runtime, and the example intentionally omits a brand-construction function because a safe constructor must define how identifiers are obtained or validated.
Branded types can help prevent accidental argument swaps in application code. However, type assertions can bypass these checks, so brands are not runtime validation and should not replace verification of identifiers received from external systems.
10. Intersection Types vs Union Types
Intersections and unions are opposites in how they combine requirements. An intersection requires a value to satisfy all combined types, while a union allows a value to satisfy one of its alternatives.
type Admin = {
name: string;
permissions: string[];
};
type Member = {
name: string;
membershipLevel: number;
};
type AdminOrMember = Admin | Member;
type AdminAndMember = Admin & Member;
const admin: AdminOrMember = {
name: "Asha",
permissions: ["manage-users"]
};
const combined: AdminAndMember = {
name: "Rahul",
permissions: ["manage-courses"],
membershipLevel: 2
};
console.log(admin.name);
console.log(combined.membershipLevel);
AdminOrMember accepts an Admin or a Member. A value can satisfy both types as well, but it only needs to satisfy one alternative for assignment.
AdminAndMember requires permissions and membershipLevel along with the shared name property. It combines the obligations from both types.
Use a union when a concept can take one of several forms. Use an intersection when a value needs several capabilities or property sets at the same time. The correct choice depends on the data contract you want to describe.
11. Common Mistakes with Intersection Types
Combining conflicting property types
If one type requires a property to be a string and another requires it to be a number, their intersection cannot provide an ordinary value for that property. Ensure shared properties represent compatible requirements before combining types.
Assuming an intersection selects one type
An intersection does not choose between its alternatives. It combines the requirements from every member type. Use a union when either alternative is sufficient.
Making function parameters unnecessarily restrictive
Requiring many object capabilities limits which values a function accepts. Define a parameter using only the properties the function actually needs.
Assuming intersections change runtime objects
An intersection type combines static type requirements. It does not automatically add properties, merge objects, or transform data at runtime. The implementation must actually supply the properties it promises.
Ignoring overlapping properties in generic helpers
Adding properties through object spread can be problematic when the input already has a property with the same name but a conflicting type. Establish a clear contract for overlapping properties.
Using intersections instead of a clearer object model
Deeply nested intersections can become difficult to understand and debug. Introduce meaningful aliases, split responsibilities, or use an interface declaration when that better communicates the intended model.
Best Practices for Intersection Types
- Use intersections to combine compatible contracts and reusable object shapes.
- Give meaningful names to frequently reused component types instead of repeating long intersection expressions.
- Check shared property types before combining interfaces or aliases.
- Prefer interface extends when defining a named interface based on other compatible interfaces.
- Use intersections when composing type aliases, object extensions, or more complex type expressions.
- Use unions rather than intersections when a value needs to satisfy only one of several alternatives.
- Keep function parameters as narrow as practical so functions remain reusable.
- Consider branded types when structurally similar primitive values must remain distinct.
- Ensure runtime implementations truly provide all properties declared by an intersection type.
- Validate external data at runtime instead of relying on static type declarations alone.
Conclusion
Intersection types allow TypeScript to combine multiple requirements into a single type. They are useful for composing user models, adding metadata, combining capabilities, building generic helpers, and defining reusable contracts.
The most important rule is that an intersection requires all combined types to be satisfied. Compatible properties combine naturally, while conflicting property types can create an uninhabitable type or otherwise make the model difficult to use.
Practise combining interfaces and aliases, extending existing object types, using generic intersections, and comparing intersections with unions. By choosing compatible contracts and keeping type expressions understandable, you can use intersections to build maintainable, reusable TypeScript applications.