3 ms·
I wish enum was never part of TypeScript. It’s this one odd thing unlike the rest. Am I forgetting any or is it the only feature that actually generates code r
by Waterluvian 2y ago
I wish enum was never part of TypeScript. It’s this one odd thing unlike the rest.
Am I forgetting any or is it the only feature that actually generates code rather than just being extra annotation?
- riiii 2y agoAgreed. It looks OK on the surface, but if you need enums then TS enums is going to end up in frustration.
- paulddraper 2y agoLots of TypeScript features generate code: namespace, async/await, yield, decorators, object spread, varargs, etc.* `const enums` (beyond enums) are especially odd. They rely on type information to emit code. * Subsequent to their release in TypeScript, EcmaScript versions now support some of these.
- wk_end 2y agoSo, since they're now a part of JS - aside from namespaces and enums (which are discouraged) - none of those features generate code anymore. Moreover, aside from namespaces and enums (which are discouraged), none of those features were incorporated into TypeScript until they were on track for inclusion into JS. They weren't taken from TypeScript and imported into JS; TypeScript took them from the ECMAScript people. Being able to compile TypeScript by just stripping types has become a design principle of the language. For better or for worse, the TypeScript team steadfastly refuses to try to innovate on runtime language features at this point.
- paulddraper 2y agoDecorators and decorator metadata. > For better or for worse, the TypeScript team steadfastly refuses to try to innovate on runtime language features at this point. Yes, largely because ECMAScript has advanced so far relatively to where it used to be.
- wk_end 2y agoNamespaces do as well, but I think it's just those two.
- Waterluvian 2y agoOh right namespaces. Though I think those are considered deprecated? Or maybe just discouraged.
- shepherdjerred 2y agoHere's a good video about TS enums: https://www.youtube.com/watch?v=jjMbPt_H3RQ https://www.youtube.com/watch?v=jjMbPt_H3RQ
- zarzavat 2y agoStrong disagree. enums + unions = ADTs. Without enums you have to use either string or number. The problem with using strings is that nothing else uses strings (like say, your database), so you have to have this interminable roundtrip conversion between strings and integers. JS programmers are in a bit of a bubble where using strings as enums is socially acceptable, if a C programmer saw this code typedef struct { const char* type; } Animal; they would yell at you. If a database engineer saw this schema: CREATE TABLE animal ( id INTEGER PRIMARY KEY, type TEXT ) they would yell at you. It's way nicer to use integer enums from A to Z, because everything else has enums and people use them. If you use plain integers instead of integer enums then the compiler has no visibility into what your tags are. So your diagnostics will be hard to read because the compiler doesn't know that 69 is AnimalType.frog
- chpatrick 2y agoI think string enums are actually totally okay considering they're interned and basically just pointers at the VM level, and then when you're debugging you don't need to figure out what the hell 0x72838ABC is. Pointing to a lower level language with a different use case doesn't justify int enums to me.
- Waterluvian 2y agoThat’s pretty much the last straw for why I switched to string unions. Debugging integers in logs was terrible.