TypeScript union types are 🔥


TypeScript union types are 🔥.

Example:
type Status = "Open" | "Pending" | "Closed";

However, what if I want to be able to iterate over these too?

Here's a handy trick:
1. Declare an array instead
2. Use typeof to generate a union type from the array 😎

#typescript

You might wonder "Why can't I just iterate over a Union type?

Answer: Because there's no manifestation of a Union type in JavaScript. It's compiled out. So instead, I declare an array, and derive the Union type from the array.

Best of both worlds! 🤓

View original on X