DEV Community

Discussion on: We don't have to use Enums on TypeScript? 【Discussion】

Collapse
 
jackmellis profile image
Jack

While I agree that some usecases for enums could be replaced with union types, I can think of a few where enums are a much better fit.

enum ApiKeys {
  Facebook: 'somereallylongkeythatyouwouldntwanttohavetotypeoutorrepeatinmanyplaces',
}
Enter fullscreen mode Exit fullscreen mode
enum Network {
  Mainnet: 1,
  Poygon: 137,
  Rinkeby: 4
}
Enter fullscreen mode Exit fullscreen mode

I've seen many cases where we've had a collection of long, ugly, or cryptic strings. Unlike the keys of an enum, a union type does nothing to help describe what a certain string is actually for. Another fairly common usecase is when we have numeric values, if I had to type the network number every time I would waste so much time having to double check the numbers are correct...