Earlier quoted context omitted.
Numeric enums are a lot more useful than string enums, in my view.
I like string enums, since they are self-documenting, I guess int enums are smaller when sent over network. Could you expand on that?
TypeScript Features to Avoid
71–80 of 212 posts
Re: TypeScript Features to Avoid
#72For people that don’t see the problem and are happily using these features, here’s an explanation of the second problem with these sorts of features, additional to the “it’s not just JavaScript with types which is what the label said” reason which is the focus of the article. The real trouble occurs when TypeScript implements something because that looks like the way things are heading, but then they don’t head that…
Did the other feature have anything to do with modules? If so, I can't really blame TypeScript too much for that, since the JavaScript ecosystem is pretty fragmented there and TypeScript has to support all the different things that are in use with a reasonable interop story. Build tools that work purely with JavaScript source code also have to deal with this problem. Otherwise I'm not aware of any cases besides decor…
Re: TypeScript Features to Avoid
#73This whole thing feels basically grounded in purity over practicality. In general it's a good idea to write idiomatic TypeScript. Even when I agree with the given recommendations, the given reasons don't seem like the strongest ones. I most strongly disagree with the recommendation against enums. Realistically, you will probably never run into a compiler bug from enum emit; maybe something like this might happen with…
> you will probably never run into a compiler bug from enum emit; That's true, but diagnosing other bugs is an absolute pain in the butt when your enum value at runtime is 0, 1, or 2. You get all of the readability of C with none of the performance :)
Re: TypeScript Features to Avoid
#74Earlier quoted context omitted.
That's precisely the problem. It makes the enum invisible to JavaScript users.
Yes, as it should be. There are no enums. Instead, you can pass in any of the documented strings (or whatever the base type is). Even inside TypeScript, an enum is really just a union type on steroids. When you expose TypeScript code to JavaScript consumers you absolutely must validate all incoming data anyway, whatever type you declare.
Re: TypeScript Features to Avoid
#75There are features of the type system you should definitely avoid unless you're writing a library. Enums aren't it.
Re: TypeScript Features to Avoid
#76Re: TypeScript Features to Avoid
#77Earlier quoted context omitted.
I like string enums, since they are self-documenting, I guess int enums are smaller when sent over network. Could you expand on that?
In the use cases where I most typically use enums, I don't want to think about the question of runtime representation; I just want a set of arbitrary symbols that are different from one another. Implicitly initialized numeric enums do this idiomatically and concisely.
If you just use the value within your code the runtime representation does not matter, you're completely right.
Re: TypeScript Features to Avoid
#78The reasoning behind most of this, that the output JS is not a strict subset of the input is silly and imho represent a misunderstanding of what Typescript is going for. It’s a full fledged language that compiles to readable JS, not just annotated JavaScript . There are many features in Typescript where it simply isn’t just outputting a subset of the input, and many of them are the best parts of Typescript. If you ju…
It's pretty what TypeScript's going for though:
> Avoid adding expression-level syntax.
https://github.com/Microsoft/TypeScript/wiki/TypeScript-Desi...
Re: TypeScript Features to Avoid
#79Earlier quoted context omitted.
Yes, as it should be. There are no enums. Instead, you can pass in any of the documented strings (or whatever the base type is). Even inside TypeScript, an enum is really just a union type on steroids. When you expose TypeScript code to JavaScript consumers you absolutely must validate all incoming data anyway, whatever type you declare.
This is not about trusting the information your users provide. It is about making your APIs accessible to users. If you hide some information, your API is less accessible.
Re: TypeScript Features to Avoid
#80Earlier quoted context omitted.
esbuild seems to support enums fine
It doesn't support one variant of them, const enums for example. That ties you to tsc emit. Its pretty clear that if the tsc team could they would remove enums and favour literal unions.