TypeScript Union and Intersection Types
Understanding the difference between union and intersection types in TypeScript with objects.
Yaswanth Gudivada
October 02, 2026 · 9 min read
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:
A = {1, 2, 3}
B = {3, 4, 5}
Mathematical Union: A ∪ B#
A union contains everything that belongs to A or B.
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.
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 (|):
type Animal = "cat" | "dog";This means that a value can be either "cat" or "dog".
let animal: Animal;
animal = "cat"; // Valid
animal = "dog"; // ValidThe following value is not allowed:
animal = "bird"; // ErrorSo this type:
"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 (&):
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.
type Animal = "cat" & "dog"; // Impossible typeWith 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:
type Person = {
name: string;
};
type Employee = {
employeeId: number;
};Now create an intersection:
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:
{
name: string;
}The Employee type requires an employeeId property:
{
employeeId: number;
}Therefore, Person & Employee requires both properties:
{
name: string;
employeeId: number;
}For example:
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:
- Which properties do these types have in common?
- Which values satisfy both types?
An intersection type answers the second question.
For example:
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:
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:
type Person = {
name: string;
};This type represents many valid objects:
{ 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:
type Employee = {
employeeId: number;
};represents many objects containing a numeric employeeId.
When we write:
type PersonEmployee = Person & Employee;we are selecting values that belong to both groups. Such values must contain:
{
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:
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:
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:
type A = {
name: string;
};
type B = {
age: number;
};Union Types Example#
type C = A | B;A value of type C can be:
{ name: "Alice" }or:
{ age: 20 }or:
{ name: "Alice", age: 20 }The value needs to satisfy at least one of the two types.
Intersection Types Example#
type C = A & B;A value must satisfy both types:
{
name: "Alice",
age: 20,
}Both properties are required.
Why Does a Union Feel Limiting?#
Consider these types:
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.
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:
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:
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:
type User = {
name: string;
};
type Admin = {
admin: boolean;
};
type UserAdmin = User & Admin;A value of type UserAdmin must contain both properties:
const userAdmin: UserAdmin = {
name: "Alice",
admin: true,
};Because both properties are guaranteed, you can access them directly:
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
AorB.
Because you do not know which type you have, fewer properties are guaranteed.
TypeScript Intersection: A & B#
The value satisfies both
AandB.
Because both types are guaranteed, more properties are available.
Another way to remember this is:
Union:
More possible values
Less information about each value
Intersection:
Fewer possible values
More information about each valueExample with Cats and Dogs#
Consider these two types:
type Cat = {
meow: () => void;
};
type Dog = {
bark: () => void;
};Using a Union#
type Animal = Cat | Dog;An Animal can be either a Cat or a Dog.
You cannot safely call either method without checking first:
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:
function makeSound(animal: Animal) {
if ("meow" in animal) {
animal.meow();
}
if ("bark" in animal) {
animal.bark();
}
}Using an Intersection#
type SuperAnimal = Cat & Dog;A SuperAnimal must have both methods:
const superAnimal: SuperAnimal = {
meow: () => {
console.log("Meow");
},
bark: () => {
console.log("Bark");
},
};Both methods can be called directly:
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:
A | B // A or B
A & B // A and BFor object types:
type A = { name: string };
type B = { age: number };
type Union = A | B;
// Requires name or age
type Intersection = A & B;
// Requires both name and ageOnce you start thinking in terms of valid values and required properties, TypeScript’s union and intersection types become much easier to understand.
Dual Boot Ubuntu 26.04 LTS with Windows 11: A Complete Guide
A step-by-step guide to dual booting Ubuntu 26.04 LTS alongside Windows 11, including disk partitioning, creating a bootable USB, installation, GRUB configuration, and troubleshooting.
Downloading and Installation of Java version 8
Learn why we need Java software, how to download and install it from Oracle, and the crucial steps for setting up environment variables permanently.