|
|
|
|
|
by brundolf
1735 days ago
|
|
My recommendation is to not use enums; use plain string unions instead. There's no wondering about how they compile or serialize (or deserialize), there's no importing/exporting (except the types), you even get autocomplete. And TypeScript makes them every bit as safe as enums (more so, based on some things I learned in this article). |
|
The only downside so far is that `[Example.First, Example.Second].includes(user.example)` no longer works because the array's type is narrowed to the tuple `("first", "second")` instead of an array of Examples `Example[]` without casting: `([Example.First, Example.Second] as Example[]).includes(user.example)` or worse `[Example.First, Example.Second].includes(user.example as any)`. Because this is an extremely common pattern for us, we've taken to using the lodash function `includes` which is typed differently but performs the exact same check under the hood.