Hacker News new | ask | show | jobs
by conaclos 1608 days ago
Replacing TS enum with JS objects is really tedious:

  type HttpMethod = keyof typeof HttpMethod

  const HttpMethod = {
      GET: "GET",
      POST: "POST",
  } as const
It is even worse with int enum:

  type HttpMethod = Extract<keyof typeof HttpMethod, number>

  const HttpMethod = {
      GET: 0,
      POST: 1,
      0: "GET",
      1: "POST",
  } as const
My personal "avoid"-rules are:

- Avoid enum with negative integers because it generates code that cannot be optimized by the JS engine

- Prefer ESM modules to namespaces, because the first can be tree-shaked.

1 comments

I've been using the method I outlined here [0] and it's not tedious to work with at all. That said you might have a different usecase where my example doesn't work.

[0] https://news.ycombinator.com/item?id=30010017