Union and Intersection Types
Unions say a value is one of several types, and intersections say it is all of them at once. See how each behaves, where it fails and how to narrow it.

Two operators build most of the interesting types in TypeScript. The pipe, A | B, makes a union: the value is an A or a B. The ampersand, A & B, makes an intersection: the value is an A and a B at the same time. The names sound like set operations on values, and that is correct, but object types add a twist that trips people up. A union of two objects lets you read only the properties both share, while an intersection of two objects has all the properties of both. This post covers how each works, the exact errors you will see, how to narrow a union into something usable, and the cases where an intersection quietly turns into never. It uses TypeScript 6.x with strict on, and follows the handbook on unions and intersections and the object types page.
A union is one of several types
A union accepts a value that fits any member. The classic use is an identifier that might arrive as text or as a number.
type Id = string | number;
function show(id: Id): string {
return id.toUpperCase();
}union-basic.ts(4,13): error TS2339: Property 'toUpperCase' does not exist on type 'Id'.
Property 'toUpperCase' does not exist on type 'number'.The compiler allows only operations that are valid for every member. toUpperCase exists on strings and not on numbers, so the call is refused, and the second line of the message names the member that breaks it. This feels restrictive at first, but it is exactly the protection you want: if the function received 42 the call would throw at runtime.
The fix is to narrow the union before using a member-specific operation.
type Id = string | number;
function show(id: Id): string {
if (typeof id === "string") {
return id.toUpperCase();
}
return id.toFixed(0).padStart(4, "0");
}
console.log(show("ab12"));
console.log(show(7));AB12
0007Inside the if, id is a string. After the if returns, the compiler removes string from the union, so the rest of the function sees a number. The technique is called narrowing, and the next post in this series covers it in depth, but for unions the idea is simple: each check shrinks the set of possible members.
Unions of object types share only what they have in common
When the members are objects, the properties you can read without narrowing are the ones present in all of them.
type Email = { channel: "email"; address: string };
type Sms = { channel: "sms"; number: string };
type Contact = Email | Sms;
function label(contact: Contact): string {
return contact.channel;
}
function target(contact: Contact): string {
return contact.address;
}union-objects.ts(10,18): error TS2339: Property 'address' does not exist on type 'Contact'.
Property 'address' does not exist on type 'Sms'.channel is shared, so label compiles. address exists only on Email, so target fails. Because channel holds a different literal in each member, it can act as a tag. Checking it narrows the whole object. This pattern is a discriminated union.
type Email = { channel: "email"; address: string };
type Sms = { channel: "sms"; number: string };
type Contact = Email | Sms;
function target(contact: Contact): string {
switch (contact.channel) {
case "email":
return contact.address;
case "sms":
return contact.number;
}
}
console.log(target({ channel: "email", address: "[email protected]" }));
console.log(target({ channel: "sms", number: "+15550100" }));[email protected]
+15550100In each case the compiler knows which member it has. The function has no default, and it still compiles because the cases cover the union, so the end of the function is unreachable. Add a third member and the function gains an implicit undefined return, which strict reports. Discriminated unions are the most useful application of union types in everyday code: they model states, messages, API responses and events, and they let one switch handle each case with full type information.
An intersection is all of the types at once
An intersection combines types. For objects, the result has every property from every member.
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
const ada: Person = { name: "Ada", age: 36 };
console.log(`${ada.name} is ${ada.age}`);
const partial: Person = { name: "Grace" };intersection-basic.ts(8,7): error TS2741: Property 'age' is missing in type '{ name: string; }' but required in type 'Aged'.The first assignment passes and prints Ada is 36. The second fails because a Person must satisfy both Named and Aged. This is the opposite of the union behavior: with a union you read less, and with an intersection you must provide more.
Intersections are the usual way to add fields to an existing type without editing it, and they work well with generics.
type Audited = { updatedBy: string };
function withAudit<T extends object>(item: T, updatedBy: string): T & Audited {
return { ...item, updatedBy };
}
const invoice = withAudit({ id: 1, total: 120 }, "ada");
console.log(invoice.total, invoice.updatedBy);
console.log(invoice);120 ada
{ id: 1, total: 120, updatedBy: 'ada' }The return type T & Audited tells callers that every property of the input is still present and that updatedBy has been added. The compiler accepts the spread because the spread of a generic T and an object literal is typed as the intersection of the two.
When an intersection becomes never
An intersection of primitives with no overlap has no possible values, so TypeScript reduces it to never. Object types with conflicting property types follow the same logic for that property.
type FromForm = { id: string };
type FromDatabase = { id: number };
type Merged = FromForm & FromDatabase;
function read(record: Merged): number {
const id: never = record.id;
return id;
}
const record: Merged = { id: 1 };conflict.ts(10,26): error TS2322: Type 'number' is not assignable to type 'never'.The type Merged still exists, and it still has an id property, but that property's type is string & number, which is never. The declaration of read compiles, since record.id has type never and a never is assignable to itself. The error appears only when you try to build a value, because nothing can be assigned to never. This is a common surprise when two sources describe the same field differently. If you want to replace a field rather than combine it, use Omit, as in Omit<FromForm, "id"> & { id: number }, or define the shared shape once and extend it.
Unions and intersections together
Mixing the two operators works as you would expect from algebra, because intersection distributes over union. (A | B) & C is the same as (A & C) | (B & C). This is useful for adding a shared field to each member of a union.
type Email = { channel: "email"; address: string };
type Sms = { channel: "sms"; number: string };
type Meta = { sentAt: string };
type Delivered = (Email | Sms) & Meta;
const items: Delivered[] = [
{ channel: "email", address: "[email protected]", sentAt: "morning" },
{ channel: "sms", number: "+15550100", sentAt: "noon" },
];
for (const item of items) {
console.log(item.channel, item.sentAt);
}email morning
sms noonDelivered is still a discriminated union, since each member keeps its channel tag, and every member carries sentAt. A narrowing switch on channel works exactly as before. The result is a compact way to attach metadata without repeating it in each member type.
Arrays and optional values
Two small unions appear everywhere. T | undefined models a value that may be missing, and T | T[] models an argument that may be one item or a list. The second one needs a small normalizing step, because the two shapes behave differently.
function toList<T>(input: T | T[]): T[] {
return Array.isArray(input) ? input : [input];
}
console.log(toList("a"));
console.log(toList(["a", "b"]));[ 'a' ]
[ 'a', 'b' ]Array.isArray narrows the union in both directions: the true branch has the array and the false branch has the single item. A caveat applies when T itself might be an array, because the check cannot tell the difference at runtime. In that case, accept two separate overloads or a clearer parameter type.
Choosing between them
Use a union when a value can take different forms and the code branches on which one it got. Use an intersection when you are building a bigger type from smaller ones, or when a function adds properties to its input. Prefer a discriminated union to a bag of optional properties: a type like { error?: string; data?: User } allows both fields or neither, while { ok: true; data: User } | { ok: false; error: string } allows exactly the two states that exist.
Interfaces can also compose with extends, and the differences between that and intersections are covered in the post on type aliases and interfaces. For most day-to-day modeling, the rule is short: unions for alternatives, intersections for composition, and a literal tag whenever you need to tell the alternatives apart.
Testing a union
A type-level change needs a test too. For a discriminated union, the most useful test is a table that feeds one value per member through the function and checks the result.
import { strictEqual } from "node:assert/strict";
type Email = { channel: "email"; address: string };
type Sms = { channel: "sms"; number: string };
type Contact = Email | Sms;
function target(contact: Contact): string {
return contact.channel === "email" ? contact.address : contact.number;
}
const cases: Array<[Contact, string]> = [
[{ channel: "email", address: "[email protected]" }, "[email protected]"],
[{ channel: "sms", number: "+15550100" }, "+15550100"],
];
for (const [input, expected] of cases) {
strictEqual(target(input), expected);
}
console.log(`${cases.length} cases passed`);2 cases passedWhen someone adds a third member, the compiler flags the function, and the table is where the new case goes. The compile error finds the code that needs changing, and the table proves that it changed correctly.


