TypeScript
TypeScript Union TypesTypeScript Union Types
Union types allow a variable to hold multiple specific types.
Read Time
5 min readDifficulty
BeginnerLast Updated
Jun 15, 2026Version
v1.0.0Introduction
Sometimes a variable legitimately needs to handle two different types of data. For example, a phone number might be a number (`5551234`) or a formatted text string (`"555-1234"`). Union types solve this problem.
Example
Example
Key Points
- Use the pipe symbol `|` to create a union.
- It means "either this OR that".
- It gives flexibility without losing strict safety.
- You often have to check the type before using the data.