Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I would actually love a “Typescript: The good parts” as a spiritual sequel to Javascript: The good parts. [0] Something that starts at the basics and builds upon that knowledge layer by layer like this handbook, but doesn’t dedicate more than a page to any one topic, and doesn’t repeat itself. Not a tutorial, but more like a glossary ordered by abstraction.

Plus, there’s a lot of Typescript I would consider not part of the “good stuff”, we can leave out enums.

[0] https://www.goodreads.com/book/show/2998152-javascript



Try Dan Vanderkam's "Effective Typescript: 62 Specific Ways to Improve YourTypeScript" (O'Reilly). I'm no TS guru, but IMHO it's pretty well-grounded in first principles.


What's wrong with enums? Enum types in general are very useful.


If you mean traditional enums, called unions in Typescript and represented by the pipe symbol, then nothing. But if you mean the named Enum keyword in Typescript, then the problem is that they emit runtime code and aren't type safe.


Amongst their issues is the issue of enum vs const enum. These compile to different outputs and support different behaviors. It’s not possible to compile a TS file that uses them without looking at the enum declaration to see if it’s const. It’s no longer possible to strip the types and have something that any JS bundler can understand.


That seems like a limitation that makes sense. A non-const enum would need a lot more complexity to be extensible at runtime.


Someone mentioned they shouldn't be in TS altogether in another thread

shrugs

I like em too


> What's wrong with enums?

They don't compile down to just the same code with the type annotations and definitions removed, which is often considered to be the ideal that typescript should follow. Instead, they compile down to some more complicated blob of javascript. People critique and avoid typescript's namespaces for the same reason.

> Enum types in general are very useful.

You can use unions for the same purpose without the aforementioned drawback, and unions have other benefits which typescript's enums don't offer, like support for more complex data types, which makes unions closer to set of features more modern languages tend to offer via enums, while making enums closer to the barebones enums found in languages like C and C++ (effectively just a set of named constants enclosed into a type).


I guess I don't value the "TS transpiles to JS 1-to-1" property very much.

Unions are useful for quite a few things. But I literally was talking about the barebones (and thus simple to write) C/C++ style enums. Where I know it can be one of several primitive values and not have to spend the time creating a class for each possible value.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: