Union and Intersection Types in TypeScript
Combining types in TypeScript: union types (|), intersection types (&), common property access, and discriminated unions.
Union and Intersection Types in TypeScript
TypeScript provides two powerful operators for combining multiple types: Unions (|) and Intersections (&).
1. Union Types (| β "OR")
A union type represents a value that can be any one of several types:
type Identifier = string | number;
function printId(id: Identifier) {
// TypeScript only permits operations common to both string and number:
console.log(`ID is: ${id}`);
// Narrowing is required for type-specific methods:
if (typeof id === "string") {
console.log("Uppercase ID:", id.toUpperCase());
}
}2. Intersection Types (& β "AND")
An intersection type combines multiple types into one unified type containing all properties of the combined members:
type Timestamps = {
createdAt: Date;
updatedAt: Date;
};
type UserData = {
id: string;
name: string;
};
// Merged type containing all 4 properties:
type DatabaseUser = UserData & Timestamps;
const user: DatabaseUser = {
id: "usr_1",
name: "Alex",
createdAt: new Date(),
updatedAt: new Date(),
};3. Discriminated Unions (Tagged Unions)
Discriminated unions are the single most powerful architectural pattern in TypeScript for modeling state:
type NetworkState =
| { status: "loading" }
| { status: "success"; data: string[] }
| { status: "error"; error: string };
function renderUI(state: NetworkState) {
switch (state.status) {
case "loading":
return "Loading spinner...";
case "success":
// TypeScript KNOWS state.data exists here!
return `Loaded ${state.data.length} items.`;
case "error":
// TypeScript KNOWS state.error exists here!
return `Error: ${state.error}`;
}
}Related Topics
Functions and Signatures in TypeScript
Typing functions in TypeScript: parameter types, return type inference, optional/default parameters, function types, and function overloads.
Literal Types and 'as const' in TypeScript
Mastering string, numeric, and boolean literal types in TypeScript: restricting variables to exact values, template literal types, and as const assertions.