Why TypeScript Enums Are Terrible But Union Types Are Great

A deep dive into the pitfalls of TypeScript Enums and the power of Union Types, or why I refactor all my Enums to Union Types.

Dr. Derek Austin 🥳
Totally TypeScript
Published in
7 min readApr 13, 2023

--

Photo by Tatiana Rodriguez on Unsplash

Introduction: TypeScript Enums vs. Union Types

As developers, we’re constantly looking for ways to improve our codebases and create more maintainable, flexible, and robust software. One of the best ways to achieve this is by leveraging the strengths of different programming paradigms and language features. In this article, we’ll explore why TypeScript Enums are a suboptimal choice for many use cases and how Union Types can offer a superior alternative.

The Multiple Problems with TypeScript Enums

Enums, short for enumerations, are a concept borrowed from languages like C# and Java. They allow developers to define a set of named constants that represent a specific type. At first glance, Enums in TypeScript seem like a great way to improve type safety and code readability. However, they come with a set of drawbacks that may outweigh their benefits in certain scenarios.

Runtime and Compilation Overhead

--

--

Dr. Derek Austin 🥳
Totally TypeScript

Hi, I'm Doctor Derek! I've been a professional web developer since 2005, and I love writing about programming with JavaScript, TypeScript, React, Next.js & Git.