Intersection Types
TypeScript intersection types let you combine two or more types into one that has every feature of all of them. You write an intersection with the & operator. Where a union says "this or that," an intersection says "this and that." This is useful for merging object shapes, adding shared fields, and composing small types into bigger ones.
This tutorial explains the & operator, how to combine object types, real uses like mixins and timestamp fields, and the difference between intersection and union. You will also see what happens when two types have conflicting property types and why the result becomes never.
What Are TypeScript Intersection Types?
A TypeScript intersection type combines two or more types into one type that has all the properties of each. It uses the & operator. A value of an intersection type must satisfy every member type at once.
Here is a simple example:
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
const person: Person = { name: "Riya", age: 28 };
console.log(person);
// Output: { name: 'Riya', age: 28 }
The Person type requires both name and age. The object satisfies both, so it compiles.
How Do You Combine Two or More Object Types?
You can chain the & operator to combine as many types as you need. Each type contributes its properties, and the resulting type must include all of them. This is called merging object types.
type Contact = { email: string; phone: string };
type Address = { city: string; pincode: string };
type Profile = { username: string };
type Account = Contact & Address & Profile;
const account: Account = {
email: "arjun@example.com",
phone: "9876543210",
city: "Pune",
pincode: "411001",
username: "arjun_k",
};
console.log(account.username, account.city);
// Output: arjun_k Pune
How it works: The Account type merges all properties from Contact, Address, and Profile. The compiler flattens them into one shape and checks the object against it. Missing any property or using the wrong type would produce an error. This is handy when a function needs data from several sources but you do not want a single wide interface.
What Are Practical Uses of Intersection Types?
Intersections shine when you want to add shared fields to a base type without editing it. A common case is adding timestamps like createdAt and updatedAt to every record. Another is building mixins: small reusable pieces of behavior or data that you combine into one type.
type BaseEntity = { id: number };
type Timestamps = { createdAt: Date; updatedAt: Date };
type UserRecord = BaseEntity & Timestamps & { name: string };
const user: UserRecord = {
id: 1,
name: "Sana",
createdAt: new Date("2025-01-10"),
updatedAt: new Date("2025-02-15"),
};
console.log(user.id, user.name);
// Output: 1 Sana
How it works: BaseEntity provides id, Timestamps provides the two date fields, and the inline object adds name. The intersection type requires all four. If you later add deletedAt to Timestamps, every record that uses it picks up the change automatically. This keeps shared fields in one place.
What Happens with Conflicting Property Types?
If two types in an intersection have a property with the same name but different types, the property becomes never. No value can satisfy both types at once, so the intersection is impossible to build. This is the compiler's way of warning you that the merge cannot work.
type A = { value: string };
type B = { value: number };
type AB = A & B;
// const broken: AB = { value: "text" };
// Error: Type 'string' is not assignable to type 'never'.
// const alsoBroken: AB = { value: 42 };
// Error: Type 'number' is not assignable to type 'never'.
How it works: The value property in A is a string, and in B it is a number. The intersection says value must be both a string and a number, which is impossible. TypeScript narrows the property type to never because no value fits. You cannot create a valid AB object. To fix this, either rename the properties or use a union if you only need one shape at a time.
Intersection vs Union: What Is the Difference?
The two operators look similar but mean opposite things. A union says "one of these." An intersection says "all of these." The table below makes the difference clear.
| Feature | Union (|) |
Intersection (&) |
|---|---|---|
| Meaning | Either type A or type B | Both type A and type B |
| Required properties | Only shared properties are safe | All properties from every member |
| Value construction | Any member is valid | Must satisfy every member |
| Typical use | Alternatives, statuses | Merging shapes, mixins |
| Conflicting properties | No conflict, both are allowed | Becomes never |
| Narrowing | Needed to access member fields | Not needed, all fields exist |
A union of { a: string } and { b: number } accepts either { a: "x" } or { b: 1 }. An intersection of the same two requires { a: "x", b: 1 }. You can learn more union patterns in the Type Aliases & Union Types tutorial.
Intersection vs Interface Extends: Which Should You Choose?
Both intersections and interface extends can merge object types. They behave similarly for simple cases, but differ in flexibility and output. The table below compares them.
| Feature | Intersection (&) |
Interface extends |
|---|---|---|
| Works with type aliases | Yes | Only with interfaces or object type aliases |
| Merge two object types | Yes | Yes |
| Add fields to a base shape | Yes | Yes |
| Declaration merging | No | Yes |
| Works with unions | Yes (can combine) | No |
| Error messages | Can be harder to read | Clearer, points to the interface |
| Best for | Ad-hoc merges, mixins, functions | Named contracts that grow over time |
A practical rule: use extends when you are building a named object contract that will grow, especially in a public API. Use & when you want to merge types inline, build mixins, or combine a type alias with something else. The TypeScript Interfaces tutorial covers the extends side.
Common Mistakes and Best Practices
- Combining types with the same property name and different types. The property becomes
neverand the object cannot be built. Rename one property or use a union if you only need one shape. - Using
&when you meant|. An intersection requires every property, so passing a partial object fails. If your function should accept one of several shapes, use a union instead. - Forgetting that intersections do not merge duplicate method implementations. Two method signatures with the same name must be compatible. If they differ, the result is an overload, not a merged body. Keep method shapes simple when merging.
- Overusing intersections for large objects. A chain of five or more
&types becomes hard to read. Extract a named alias or useextendsfor clarity. - Assuming
&creates a new runtime type. Intersections, like all TypeScript types, vanish at compile time. There is no runtime merge. If you need actual object merging at runtime, useObject.assignor spread syntax.
Real-World Example: Combining Person and Employee
This example combines two types with & and shows the conflicting-property case.
type Person = {
name: string;
age: number;
};
type Employee = {
company: string;
salary: number;
};
type StaffMember = Person & Employee;
const staff: StaffMember = {
name: "Meera",
age: 31,
company: "Acme",
salary: 75000,
};
function describe(member: StaffMember): string {
return `${member.name} works at ${member.company} and earns ${member.salary}.`;
}
console.log(describe(staff));
// Output: Meera works at Acme and earns 75000.
type ConflictA = { code: string };
type ConflictB = { code: number };
type Broken = ConflictA & ConflictB;
// const broken: Broken = { code: "X" };
// Error: Type 'string' is not assignable to type 'never'.
How it works, line by line. Person defines name and age. Employee defines company and salary. StaffMember is the intersection, so it requires all four properties. The staff object includes them, so it compiles. The describe function accepts a StaffMember and reads any of the four fields safely, because the intersection guarantees all of them exist. Then ConflictA and ConflictB both declare code, but with different types. Their intersection narrows code to never, so no valid object can be built. The commented line shows the compiler error. This is the key difference from a union: a union of ConflictA and ConflictB would accept either shape, but the intersection accepts neither.
Summary
- TypeScript intersection types combine two or more types with the
&operator, requiring every property. - Intersections are ideal for merging object shapes, adding shared fields like timestamps, and building mixins.
- Conflicting property types in an intersection become
never, making the object impossible to build. - A union means "either," while an intersection means "both." Choose based on what your data actually is.
- Use
extendsfor named contracts that grow; use&for ad-hoc merges and type compositions. - Intersections vanish at compile time; use spread or
Object.assignfor runtime merging.
Now that you can merge types, continue to the Literal Types tutorial to learn how to restrict a value to a small set of exact strings, numbers, or booleans.
Frequently Asked Questions
Can I intersect a union with another type?
Yes. (A | B) & C distributes over the union, producing (A & C) | (B & C). This means the value must be either A and C, or B and C. It is useful when a shared set of fields applies to every variant of a union. Wrap the union in parentheses so the & applies to the whole group.
Why does my intersection property become never?
That happens when two types declare the same property name with different types. The intersection says the property must be both types at once, which is impossible, so TypeScript narrows it to never. To fix it, rename one property or use a union if you only need one shape at a time.
Is an intersection the same as interface extends?
They behave similarly for simple object merging, but they differ in flexibility. An intersection works with any types, including unions and type aliases. extends only works with interfaces or object type aliases and supports declaration merging. Use extends for named contracts and & for ad-hoc merges.
Does an intersection merge objects at runtime?
No. All TypeScript types, including intersections, disappear when the code compiles to JavaScript. The intersection only checks the shape at compile time. If you need to merge objects at runtime, use Object.assign or the spread operator, such as { ...a, ...b }.
What is the difference between & and | in TypeScript?
& is an intersection: the value must satisfy every type, so it needs all properties. | is a union: the value must satisfy at least one type, so it can be any one of them. Use & to combine shapes and | to offer alternatives. Conflicting properties become never under & but are allowed under |.
Practice Exercises
Exercise 1 (Easy): Combine Two Simple Types
Create types HasName = { name: string } and HasAge = { age: number }. Combine them with & into Person and create one object.
Show solution
type HasName = { name: string };
type HasAge = { age: number };
type Person = HasName & HasAge;
const p: Person = { name: "Kiran", age: 27 };
console.log(p);
// Output: { name: 'Kiran', age: 27 }
The & operator requires both name and age, so the object must include both properties.
Exercise 2 (Easy): Add Timestamps to a Base Type
Create Base = { id: number } and Timestamps = { createdAt: Date }. Combine them into Record and create one object.
Show solution
type Base = { id: number };
type Timestamps = { createdAt: Date };
type Record = Base & Timestamps;
const r: Record = { id: 1, createdAt: new Date("2025-03-01") };
console.log(r.id, r.createdAt.getFullYear());
// Output: 1 2025
The intersection adds createdAt to every record without editing the base type, which is the classic timestamp pattern.
Exercise 3 (Medium): Show a Conflicting Property
Create two types with a value property, one as a string and one as a number. Combine them with & and try to assign an object. Read the error.
Show solution
type A = { value: string };
type B = { value: number };
type AB = A & B;
// const bad: AB = { value: "text" };
// Error: Type 'string' is not assignable to type 'never'.
// const alsoBad: AB = { value: 42 };
// Error: Type 'number' is not assignable to type 'never'.
The conflicting value property becomes never, so no valid object can satisfy both types at once.
Exercise 4 (Challenge): Mixin with a Function Parameter
Create two types Loggable = { log(): string } and Identifiable = { id: number }. Combine them into Entity. Write a function describe(entity: Entity) that calls both log and reads id.
Show solution
type Loggable = { log(): string };
type Identifiable = { id: number };
type Entity = Loggable & Identifiable;
function describe(entity: Entity): string {
return `${entity.log()} (id ${entity.id})`;
}
const item: Entity = {
id: 7,
log() {
return "Item created";
},
};
console.log(describe(item));
// Output: Item created (id 7)
The intersection guarantees both log and id exist, so the function can call entity.log() and read entity.id safely without narrowing.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts.
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
