What is an intersection type?
What is an Intersection Type
An intersection type combines several types into one, creating a type that contains all the properties and the requirements of each of them.
In simpler terms:
A & Bmeans: an object must match both type A and type B at the same time.
Example 1. Intersecting two objects
type User = { name: string };
type Contact = { email: string };
type UserWithContact = User & Contact;
const person: UserWithContact = {
name: "Tim",
email: "tim@example.com",
};UserWithContact now combines the fields of both types:
namefromUseremailfromContact
Example 2. Intersecting interfaces
interface A { a: number }
interface B { b: string }
type AB = A & B;
const value: AB = { a: 10, b: "hello" };You can use this with either
typeorinterface- the result is the same.
Example 3. Using it in functions
Sometimes you need to type parameters that satisfy several constraints at once:
type CanLog = { log: () => void };
type CanError = { error: (msg: string) => void };
function handleLogger(service: CanLog & CanError) {
service.log();
service.error("Something went wrong");
}
const logger = {
log: () => console.log("OK"),
error: (msg: string) => console.error(msg),
};
handleLogger(logger); // the object implements both interfacesExample 4. Intersecting with primitives
TypeScript allows you to intersect even primitive types,
but the result can be never reachable (never):
type A = string;
type B = number;
type C = A & B; // impossible - string and number do not intersect
// type C = neverIntersecting incompatible types produces
never.
Example 5. Intersecting union types
Intersection also applies to unions (|):
type A = { a: number } | { b: number };
type B = { a: number } | { c: number };
type Intersection = A & B;
// Result: { a: number } | ({ a: number } & { c: number }) | ({ b: number } & { a: number }) | ...In real practice this is rarely written by hand, but TypeScript can compute the resulting type precisely.
Example 6. Inheritance through intersection
Sometimes it is simpler to use & instead of extends:
type Base = { id: number };
type Timestamps = { createdAt: Date; updatedAt: Date };
type Entity = Base & Timestamps;
const post: Entity = {
id: 1,
createdAt: new Date(),
updatedAt: new Date(),
};This lets you build composite types out of modules, like "mixins" in OOP.
Intersection vs Union
| Operator | Name | Description |
|---|---|---|
A | B | Union | |
A & B | Intersection | The value must be A and B at the same time |
Comparison example:
type Dog = { bark: () => void };
type Cat = { meow: () => void };
type PetUnion = Dog | Cat; // either a dog or a cat
type PetBoth = Dog & Cat; // both a dog and a cat at the same timeExample 7. Intersecting with generic types
function merge<T, U>(obj1: T, obj2: U): T & U {
return { ...obj1, ...obj2 };
}
const result = merge({ name: "Tim" }, { age: 25 });
// type of result: { name: string; age: number }A very common real-world technique: a
mergefunction returns the intersection of two objects.
Important to remember
- Intersection combines properties, not values.
- If the same property has different types in the intersection, TS raises an incompatibility error:
type A = { id: number };
type B = { id: string };
type C = A & B; // Type 'string' is not assignable to type 'number'Summary
| Property | Description |
|---|---|
| Operator | & |
| Name | Intersection Type |
| Meaning | "Both at the same time" |
| Result | A type that includes all the properties |
| Opposite | ` |
| Common use | Type composition, mixins, combining interfaces |
Simple definition:
An intersection type (
A & B) is a type that combines all the fields and requirements fromAandB.An object of such a type must match both at the same time.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.