3 ms·
Replacing TS enum with JS objects is really tedious: type HttpMethod = keyof typeof HttpMethod const HttpMethod = { GET: "GET", POST: "POST",
by conaclos 5y 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.
- joshstrange 5y agoI'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 https://news.ycombinator.com/item?id=30010017