Course contentsShow
← TypeScript
Lesson 391 of 1,7969. Union TypesFree lesson

Union Order and Duplicate Types

Union type order doesn't matter, and duplicate types in a union are automatically removed.

Union Order and Duplicate Types

You'll learn: Union types automatically clean themselves up—TypeScript ignores duplicate types and doesn't care about the order you write them in.

Why Order Doesn't Matter

When you create a union type, you might worry about whether to write string | number or number | string. Good news: TypeScript treats them identically. The type checker sees both orderings as the exact same type.

type A = string | number;
type B = number | string;

// These are completely equivalent
let valueA: A = 42;
let valueB: B = 42;

valueA = "hello";
valueB = "hello";

Think of it like a set in mathematics—the set {1, 2, 3} is the same as {3, 1, 2}. Order is irrelevant to set membership.

Duplicate Types Are Removed

TypeScript is smart enough to eliminate redundant types from a union automatically. If you accidentally write the same type multiple times, TypeScript silently deduplicates them:

type Redundant = string | number | string | boolean | number;

// TypeScript internally treats this as:
// string | number | boolean

const value: Redundant = "text";  // works
const value2: Redundant = 123;    // works
const value3: Redundant = true;   // works

This makes sense—saying "it can be a string or a string" is just saying "it can be a string." The duplicate adds no information.

Why This Matters

This behavior keeps your types clean and predictable. You don't need to worry about maintaining a specific order for consistency, and you won't accidentally create "different" types by reordering unions.

Key Takeaway: Union type order is meaningless, and duplicate types in a union are automatically removed—TypeScript normalizes unions to their simplest form.