3 ms·
Unfortunately TypeScript's enums have various shortcomings compared to powerful sum types in languages like Rust: - https://stackoverflow.com/questions/4027583
by dbrgn 5y ago
Unfortunately TypeScript's enums have various shortcomings compared to powerful sum types in languages like Rust:
- https://stackoverflow.com/questions/40275832/typescript-has-unions-so-are-enums-redundant/55459814#55459814 https://stackoverflow.com/questions/40275832/typescript-has-...
- https://github.com/microsoft/TypeScript/issues/32690 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).
- IshKebab 5y agoRust can't do your second example yet either. Enum variants are not distinct types. It's a commonly requested feature though so hopefully soon...