Skip to main content

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 & B means: an object must match both type A and type B at the same time.


Example 1. Intersecting two objects

javascript
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:

  • name from User
  • email from Contact

Example 2. Intersecting interfaces

javascript
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 type or interface - the result is the same.


Example 3. Using it in functions

Sometimes you need to type parameters that satisfy several constraints at once:

javascript
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 interfaces

Example 4. Intersecting with primitives

TypeScript allows you to intersect even primitive types, but the result can be never reachable (never):

javascript
type A = string; type B = number; type C = A & B; // impossible - string and number do not intersect // type C = never

Intersecting incompatible types produces never.


Example 5. Intersecting union types

Intersection also applies to unions (|):

javascript
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:

javascript
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

OperatorNameDescription
A | BUnion
A & BIntersectionThe value must be A and B at the same time

Comparison example:

javascript
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 time

Example 7. Intersecting with generic types

javascript
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 merge function 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:
javascript
type A = { id: number }; type B = { id: string }; type C = A & B; // Type 'string' is not assignable to type 'number'

Summary

PropertyDescription
Operator&
NameIntersection Type
Meaning"Both at the same time"
ResultA type that includes all the properties
Opposite`
Common useType composition, mixins, combining interfaces

Simple definition:

An intersection type (A & B) is a type that combines all the fields and requirements from A and B.

An object of such a type must match both at the same time.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.