Exclude with String Literal Unions
What you'll learn: How to use the Exclude utility type to remove specific string literals from a union type.
The Core Idea
String literal unions are perfect for modeling fixed sets of values — like status codes, user roles, or event types. But sometimes you need a subset of that union with certain values removed. That's where Exclude shines.
Exclude<T, U> works particularly well with string literal unions because it filters out exact matches, giving you a new union with only the remaining strings.
Syntax
type AllStatuses = "draft" | "published" | "archived" | "deleted";
// Remove "deleted" from the union
type ActiveStatuses = Exclude<AllStatuses, "deleted">;
// Result: "draft" | "published" | "archived"
Removing Multiple Values
You can exclude more than one value by passing a union to the second type parameter:
type ContentStatus = "draft" | "review" | "published" | "archived";
type EditableStatuses = Exclude<ContentStatus, "published" | "archived">;
// Result: "draft" | "review"
Practical Use Case
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
// Only safe methods (no mutations)
type SafeMethod = Exclude<HttpMethod, "POST" | "PUT" | "PATCH" | "DELETE">;
// Result: "GET"
function cacheable(method: SafeMethod) {
// TypeScript knows method can only be "GET"
}
Key Takeaway: Use Exclude with string literal unions to create type-safe subsets by removing specific string values you don't want.