Despite the large amount of criticism in the comments here I think that the point the article makes here is pretty valid. The described features are not what TypeScript itself wants to be, and I think if it wasn't for backwards compatibility the team would remove some of them. IIRC namespaces as well as the `import = ` syntax come from a time where ESM wasn't a thing yet but a module system was very much needed. So n…
TypeScript Features to Avoid
61–70 of 212 posts
Re: TypeScript Features to Avoid
#62Unless you're a solo developer, what's more important it to agree on languages and conventions that apply to your team's projects. Once that's done, any change to the team's work process should be a conscious, measured decision.
Note that I also used CoffeeScript 10ish years ago and still consider it to be a superior experience to working in plain-old Javascript.
Re: TypeScript Features to Avoid
#63There 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 just want JavaScript with types, there are other languages that do that, but Typescript offers so much more.
Re: TypeScript Features to Avoid
#64Despite the large amount of criticism in the comments here I think that the point the article makes here is pretty valid. The described features are not what TypeScript itself wants to be, and I think if it wasn't for backwards compatibility the team would remove some of them. IIRC namespaces as well as the `import = ` syntax come from a time where ESM wasn't a thing yet but a module system was very much needed. So n…
One advantage of enums is that you can iterate over all possible values of an enum, but not a union type.
const MyTypeValues = ['a', 'b'] as const;
type MyType = typeof MyTypeValues[number];
MyType is now a type 'a' | 'b'Re: TypeScript Features to Avoid
#65Earlier quoted context omitted.
I see the next to zero codegen in typescript as a strategy: it removes all discutions about languages features besides typing, guarantees next to zero issues in production in case of code generation bug, making compiler deliveries safe and avoid need of coordination in toolchain.
Enums and namespaces are hardly complex code gen or generate 'issues'. People would often manually namespace in JS a few years ago, and namespaces and enums can really just be seen a lightweight holder object instances. Indeed, enums in Java are class instances. Besides, there is an straightforward way to remove enums from a program just like removing type annotations: Inline them as static fields of an object. There…
const HttpMethod = {
Get: 'GET',
Post: 'POST',
} as const;
type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod];
Maybe wrap the `{…} as const` in Object.freeze(…) for good measure.It’d be really nice if they’d improve the ergonomics on this in some way (`type Values = T[keyof T]` would reduce it to `type HttpMethod = Values`, which is a start but not enough), to make it a genuine and suitable alternative to enum (minus the other frippery that’s generated) and const enum (because it’s pure JavaScript, not an extension).
Re: TypeScript Features to Avoid
#66Earlier quoted context omitted.
But const enums are gone after compiling. They're just namespaced constants that are inlined. I don't see how this could cause any problem.
That's precisely the problem. It makes the enum invisible to JavaScript users.
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
#67Earlier 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?
Usually it's just
enum Method {
Get = "GET"
// ...
}
I usually always put doc comments on enums and their variants.Regarding why numeric ones are (only sometimes) more useful than string values: bit flags comes to mind, faster comparison of numbers than strings (not always tho... unless this is a misconception, but I don't believe so), you mentioned smaller bundle size already.
As for "auto" enums: the fact they're numbers now is an implementation detail. They could be unique Symbols in a future versions of typescript. You can do that manually now too, but I'm talking about the automatic (without assignment) syntax.
Regarding article: I... Half-agree. But I'd not completely disregard/avoid enums. At the very least, they can be useful to help model some behaviours/states in more readable, accessible, and coherent way (Other comments went into a more in-depth defence of enums, thought).
Re: TypeScript Features to Avoid
#68Earlier quoted context omitted.
I think you may have misunderstood the complaint, which is perfectly valid. A few paragraphs earlier: > The downside to enums comes from how they fit into the TypeScript language. TypeScript is supposed to be JavaScript, but with static type features added. If we remove all of the types from TypeScript code, what's left should be valid JavaScript code. The formal word used in the TypeScript documentation is "type-lev…
esbuild seems to support enums fine
Re: TypeScript Features to Avoid
#69Despite the large amount of criticism in the comments here I think that the point the article makes here is pretty valid. The described features are not what TypeScript itself wants to be, and I think if it wasn't for backwards compatibility the team would remove some of them. IIRC namespaces as well as the `import = ` syntax come from a time where ESM wasn't a thing yet but a module system was very much needed. So n…
One advantage of enums is that you can iterate over all possible values of an enum, but not a union type.