Typescript

TypeScript Union and Intersection Types

Understanding the difference between union and intersection types in TypeScript with objects.

Yaswanth Gudivada

Yaswanth Gudivada

October 02, 2026 · 9 min read

TypeScript Union and Intersection Types

TypeScript Union and Intersection Types#

TypeScript’s union (|) and intersection (&) types are often confusing at first. The confusion usually comes from the fact that these operators are based on mathematical concepts, but their behavior can feel different when they are used with object types.

The easiest way to understand them is to think about sets of possible values and the requirements a value must satisfy.

Understanding the Mathematical Meaning#

Suppose we have two sets:

text
A = {1, 2, 3}
B = {3, 4, 5}

Mathematical Union: A ∪ B#

A union contains everything that belongs to A or B.

text
A ∪ B = {1, 2, 3, 4, 5}

In other words, a union creates a larger set of possible values.

Mathematical Intersection: A ∩ B#

An intersection contains only the values that belong to both A and B.

text
A ∩ B = {3}

Therefore, an intersection creates a smaller set of possible values.

This same idea applies to TypeScript.

Union Types in TypeScript#

A union type is created using the pipe symbol (|):

typescript
type Animal = "cat" | "dog";

This means that a value can be either "cat" or "dog".

typescript
let animal: Animal;
 
animal = "cat"; // Valid
animal = "dog"; // Valid

The following value is not allowed:

typescript
animal = "bird"; // Error

So this type:

typescript
"cat" | "dog"

means:

The value can be "cat" or "dog".

This matches the mathematical meaning of a union.

Intersection Types in TypeScript#

An intersection type is created using the ampersand symbol (&):

typescript
type Animal = "cat" & "dog";

A value would need to be both "cat" and "dog" at the same time. Since that is impossible, this type cannot be meaningfully satisfied.

typescript
type Animal = "cat" & "dog"; // Impossible type

With simple literal types, TypeScript intersections behave exactly as we would expect mathematically.

However, intersections become more interesting when they are used with object types.

Intersection Types with Objects#

Consider these two types:

typescript
type Person = {
  name: string;
};
 
type Employee = {
  employeeId: number;
};

Now create an intersection:

typescript
type PersonEmployee = Person & Employee;

At first, you might think that the intersection should contain only properties shared by both types. Since Person and Employee do not have any properties in common, you might expect the result to be empty.

That is not how TypeScript works.

In TypeScript, the & operator means:

A value must satisfy both complete types.

The Person type requires a name property:

typescript
{
  name: string;
}

The Employee type requires an employeeId property:

typescript
{
  employeeId: number;
}

Therefore, Person & Employee requires both properties:

typescript
{
  name: string;
  employeeId: number;
}

For example:

typescript
const employee: PersonEmployee = {
  name: "Yaswanth",
  employeeId: 123,
};

This object is valid because it satisfies both Person and Employee.

Why Does an Intersection Add Properties?#

The important distinction is between these two questions:

  1. Which properties do these types have in common?
  2. Which values satisfy both types?

An intersection type answers the second question.

For example:

typescript
type A = {
  name: string;
};
 
type B = {
  age: number;
};

The types do not share any property names. However, a value that satisfies both types must contain both name and age:

typescript
type C = A & B;
 
const person: C = {
  name: "Alice",
  age: 25,
};

So, an intersection does not mean “keep only the common properties.” Instead, it means “combine all the requirements.”

Thinking in Terms of Valid Values#

A useful way to understand TypeScript is to think of each type as a set of valid values.

For example:

typescript
type Person = {
  name: string;
};

This type represents many valid objects:

typescript
{ name: "Alice" }
{ name: "Bob" }
{ name: "Charlie", age: 25 }

The last object is also assignable to Person because TypeScript uses structural typing. It contains the required name property, even though it has an additional age property.

Similarly:

typescript
type Employee = {
  employeeId: number;
};

represents many objects containing a numeric employeeId.

When we write:

typescript
type PersonEmployee = Person & Employee;

we are selecting values that belong to both groups. Such values must contain:

typescript
{
  name: string;
  employeeId: number;
}

The set of valid values becomes smaller, but each value contains more guaranteed information.

Union Types with Objects#

Now let us use a union instead:

typescript
type Person = {
  name: string;
};
 
type Employee = {
  employeeId: number;
};
 
type PersonOrEmployee = Person | Employee;

This means that a value can satisfy either Person or Employee.

All of these values are valid:

typescript
const person: PersonOrEmployee = {
  name: "Alice",
};
 
const employee: PersonOrEmployee = {
  employeeId: 123,
};
 
const both: PersonOrEmployee = {
  name: "Alice",
  employeeId: 123,
};

The third object is valid because it satisfies both types, and anything that satisfies both also satisfies the union.

Union vs. Intersection#

Consider these types:

typescript
type A = {
  name: string;
};
 
type B = {
  age: number;
};

Union Types Example#

typescript
type C = A | B;

A value of type C can be:

typescript
{ name: "Alice" }

or:

typescript
{ age: 20 }

or:

typescript
{ name: "Alice", age: 20 }

The value needs to satisfy at least one of the two types.

Intersection Types Example#

typescript
type C = A & B;

A value must satisfy both types:

typescript
{
  name: "Alice",
  age: 20,
}

Both properties are required.

Why Does a Union Feel Limiting?#

Consider these types:

typescript
type User = {
  name: string;
};
 
type Admin = {
  admin: boolean;
};
 
type UserOrAdmin = User | Admin;

A value of type UserOrAdmin may be a User or an Admin. Because TypeScript does not know which type it received, it does not allow you to access properties that are not guaranteed to exist on both types.

typescript
function printDetails(value: UserOrAdmin) {
  console.log(value.name); // Error
}

The error occurs because an Admin may not have a name property.

You must first narrow the type:

typescript
function printDetails(value: UserOrAdmin) {
  if ("name" in value) {
    console.log(value.name);
  }
}

Inside the if block, TypeScript knows that value has a name property.

You can also check for the admin property:

typescript
function printDetails(value: UserOrAdmin) {
  if ("admin" in value) {
    console.log(value.admin);
  }
}

A union allows more possible values, but it gives you less information about any one value until you narrow its type.

Intersections Provide More Information#

Now use an intersection:

typescript
type User = {
  name: string;
};
 
type Admin = {
  admin: boolean;
};
 
type UserAdmin = User & Admin;

A value of type UserAdmin must contain both properties:

typescript
const userAdmin: UserAdmin = {
  name: "Alice",
  admin: true,
};

Because both properties are guaranteed, you can access them directly:

typescript
function printDetails(value: UserAdmin) {
  console.log(value.name);
  console.log(value.admin);
}

No additional type narrowing is necessary.

A Practical Mental Model#

It is useful to remember the basic rules:

TypeScript Union: A | B#

The value satisfies A or B.

Because you do not know which type you have, fewer properties are guaranteed.

TypeScript Intersection: A & B#

The value satisfies both A and B.

Because both types are guaranteed, more properties are available.

Another way to remember this is:

typescript
Union:
More possible values
Less information about each value
 
Intersection:
Fewer possible values
More information about each value

Example with Cats and Dogs#

Consider these two types:

typescript
type Cat = {
  meow: () => void;
};
 
type Dog = {
  bark: () => void;
};

Using a Union#

typescript
type Animal = Cat | Dog;

An Animal can be either a Cat or a Dog.

You cannot safely call either method without checking first:

typescript
function makeSound(animal: Animal) {
  animal.meow(); // Error
  animal.bark(); // Error
}

The value may be a Cat, in which case bark does not exist. It may also be a Dog, in which case meow does not exist.

You can narrow the type with the in operator:

typescript
function makeSound(animal: Animal) {
  if ("meow" in animal) {
    animal.meow();
  }
 
  if ("bark" in animal) {
    animal.bark();
  }
}

Using an Intersection#

typescript
type SuperAnimal = Cat & Dog;

A SuperAnimal must have both methods:

typescript
const superAnimal: SuperAnimal = {
  meow: () => {
    console.log("Meow");
  },
  bark: () => {
    console.log("Bark");
  },
};

Both methods can be called directly:

typescript
superAnimal.meow();
superAnimal.bark();

Final Takeaway#

The most important sentence to remember is:

A union (|) expands the set of possible values but reduces what you can safely assume about one value. An intersection (&) reduces the set of possible values but increases what you can safely assume about one value.

In short:

typescript
A | B // A or B
A & B // A and B

For object types:

typescript
type A = { name: string };
type B = { age: number };
 
type Union = A | B;
// Requires name or age
 
type Intersection = A & B;
// Requires both name and age

Once you start thinking in terms of valid values and required properties, TypeScript’s union and intersection types become much easier to understand.

Tags:Typescript