Generally, prefer TypeScript union types over enums


Generally, prefer TypeScript union types over enums.

But, here's a rare case where enum is preferable:

If you’re calling a function that accepts a string, and you can't change the type signature, an enum provides autocomplete support. A union type doesn’t.

cc @mattpocockuk

Here's a link to the TypeScript Playground so you can see what I mean.

https://www.typescriptlang.org/play?jsx=4#code/C4TwDgpgBAysCGwCuBnAqgOwJYHsNQF4oAiHSDYqAHxIGMAbHFCAE2IG4AoTiDJAW1gJkKAKJ9BAb05RZUMr0IkFFADQy5DJqyXEtzNpwC+3APSmoASX7wA5lgzRgACywoobqPCgusAJxYoMHg-UCgAMyQMWmBcDFUoFBwrKBY8AHJgKFo8YD8ceh9nJ3BoFCxbDEQkPwhOSOjYvChGWwBZCBQUOwgACn5O7tsIAC5EvIdbAEooaTlsvCT6CAA6Vv7BnqmuE05zKABheHp6SZacds3hqAA3LG9vTDiEyyhhrIxk+CRgHBz+MDLYCrKCAXg3AHI7nFaHS6PV6pHIxG2ZgsRxOZ2hV2gdwe+F4AhebwgWW+v3+gOJINBgFkdqEXGFDPpwapiCQrFTbIA

View original on X