# Union Types

> Allowing a value to be one of several different types.

Subject: TypeScript | Lesson 10 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/union-types
Previous: [Literal Types](https://codewithanimation.in/typescript/literal-types.md) | Next: [Intersection Types](https://codewithanimation.in/typescript/intersection-types.md)

## The OR Operator for Types

In JavaScript, a variable's type can change over time. An ID might be generated as a `number` by the database, but passed around as a `string` in the URL.

TypeScript's Union Type (`|`) allows you to model this reality. It acts like an OR operator for types. `string | number` means 'this value can be a string OR a number'.

## Safety across multiple shapes

Union types are incredibly common when fetching data. An API response is often a union of a loading state, a success state (with data), and an error state (with a message).

By typing the response as a union, TypeScript forces you to handle all possible states before you can safely access the data.

## Defining and using unions

You define a union using the pipe character (`|`).

```typescript
type Identifier = string | number;

function printId(id: Identifier) {
  console.log("Your ID is: " + id);
}
```

## Common mistake: The property intersection trap

The most confusing part of union types for beginners is how TypeScript restricts property access. If you have a union of two object types, you can ONLY access properties that exist on BOTH types.

```typescript
type Cat = { name: string; meow: () => void };
type Dog = { name: string; bark: () => void };

function speak(animal: Cat | Dog) {
  animal.name; // OK (shared)
  animal.meow(); // Error: Property 'meow' does not exist on type 'Dog'.
}
```

To access `meow`, you first have to prove to TypeScript that the animal is actually a `Cat`. This is called Type Narrowing.

## Tip: Unions vs Intersections

Unions (`|`) expand the allowed values (OR), but restrict the allowed properties to only what is shared.

Intersections (`&`) restrict the allowed values (AND), but expand the allowed properties to include everything.

## Try it: Try it

Select different values to see how a union type acts as a gatekeeper, accepting valid shapes and rejecting invalid ones.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/union-types

## Check yourself

### Question 1

If you have a variable typed as 'A | B', what properties are you allowed to access on it immediately?

- A. Only properties that exist on A.
- B. All properties from both A and B.
- C. Only properties that exist on BOTH A and B.

Correct answer: C

TypeScript is pessimistic. Because the value could be EITHER A or B, it only lets you access properties that are guaranteed to exist regardless of which one it is.

- Why not A: What if the runtime value is actually B?
- Why not B: That is how Intersections (&) work, not Unions.

## Remember this

- A Union Type (`A | B`) means a value can be type `A` OR type `B`.
- You can only immediately access properties that exist on ALL members of the union.
- To access specific properties, you must 'narrow' the union type first.
