Is anyone still using the class keyword in javascript or typescript? private field syntax doesn't matter in the first place if you don't use class {} anywhere ...
I feel like most of the typescript code i've been in recently looked like it needed 0 more class declarations.
I use classes quite a lot. And I’m kind of a FP zealot. I generally treat class as a struct value type, and I also generally go to the trouble of marking every property readonly. They’re useful for several reasons:
- Abstract classes are useful for defining nominal interface types
- Classes are a clear signal that a set of methods are designed to interact with related data types
- The prototype chain can be helpful for debugging where POJOs may lose information in the call stack
- JS runtimes can sometimes optimize classes in ways they can’t with POJOs
I think the oo features tend to not play as well with many styles of functional programming -- at least the forms of it that work well in typescript ... in typescript I tend to represent data as structurally typed "plain old javascript" objects and my program becomes largely just functions that operate on values and return new values. The idiomatic ways available in the language to copy and produce new values from old values tend to be straightforward and without as many gotchas when the values themselves carry all their meaning without the prototype chain. Once the prototype chain is a important factor in the program behavior you tend to have to keep track of "how exactly was a value with this shape acquired" -- i can't as easily just roundtrip it through some json for example -- and copying a value tends to not be as composable an operation with a tree of objects where the reachable sub objects all have behavior based on prototype chain.
When the prototype chain is involved I think the value gained from structural typing tends to decrease -- or at least exposes the programmer to more sharp edges.
Also classes don’t serialize nicely so they’re a headache when dealing with things like redux actions, api endpoints, storing in local storage or a db etc. POJOs are a lot smoother.
I feel like most of the typescript code i've been in recently looked like it needed 0 more class declarations.