Hacker News new | ask | show | jobs
by dbrgn 1608 days ago
Unfortunately TypeScript's enums have various shortcomings compared to powerful sum types in languages like Rust:

- https://stackoverflow.com/questions/40275832/typescript-has-...

- https://github.com/microsoft/TypeScript/issues/32690

In a TS codebase I'm currently working on, we have the policy of never using "plain" TS enums. Instead, we have a tool that generates our own enum objects using a schema and a generator script for the cases where we want "rich" enums. For other use cases, we use string unions a lot (in combination with a helper function that allows exhaustive matching).

1 comments

Rust can't do your second example yet either. Enum variants are not distinct types. It's a commonly requested feature though so hopefully soon...