Class Contradictions in TypeScript vs JavaScript

Adam Nathaniel Davis - Jun 18 '20 - - Dev Community

Lately, I've been noticing something... odd about the fanboy wars that continually rage in the JS/React/TS communities. If you've spent even a little bit of time writing JavaScript, or browsing through the online volumes of JS blogs and thought leaders, you may have noticed some of these trends over the last several years:

  1. JavaScript is all about functional programming. To hear some of the fanboys vent on the subject, you'd almost think that it's impossible to write JavaScript without adhering to a strict FP paradigm.

  2. As a consequence of this functional-programming obsession, classes - and JS's pseudo-class keyword - are bad. Really, really bad. Don't ask why they're bad. If you even ask such a question, you risk being ostracized. So rather than risking the fanboys' wrath, just quiet down and accept that classes are bad.

  3. A lot of the more-senior, more "hardcore" JS acolytes have (by my estimation) increasingly migrated toward TypeScript in the last 3-4 years. Sure - you're not required to write TS to be a "real" frontend/JS dev. But it feels (to me) like TS is becoming a much larger influence amongst the hardcore JS devs - the same devs who seem so deeply offended by the sight of the class keyword in any of their plain-ol' JavaScript code.

  4. The previous point is amplified if you're an Angular dev. Modern Angular has become, basically, a TypeScript library.

  5. When you start writing a lot of TS, and you start talking to a lot of those grizzled JS veterans who now prefer to code only in TS, they don't seem to have too many problem with class.

     

Wait... What???

 

JS Classes Are... Bad??

I won't repeat here all of my frustrations with the "typical" JS dev who wrinkles his nose at the class keyword, but believes anything inside a function is sublime. You can see those articles linked at the top of this blog series.

My only point (in this section) is to highlight the fact that, nowadays, if you're developing in React, or in vanilla JS, or in many other JS libraries, there's a good chance that some of your team members disdain classes - and love functions. And if you don't wanna be seen as the out-of-date dinosaur on the team, you'd better shape up and start cranking out more of those functions.

TS Classes Are... Good?? (Or At Least, Acceptable??)

And maybe I could get down with the whole idea that any use of a class keyword is somehow "wrong" - if it weren't for some of the glaring contradictions that I feel exist in the JS/TS code-sphere. Specifically, I can't figure out, for the life of me, why it is that a class - in JS - is somehow antithetical to the core idea of JS. But if you switch to TS (and all JS is TS), it's perfectly fine to start cranking out classes??

You don't have to take my word for it. Spend a few minutes googling React tutorials - especially, any tutorials published in the last two-or-so years. You'll see that many of them are heavily focused on functions. (Or... on the new "hotness" - Hooks!) Then spend some time googling the latest tutorials on TS. And repeatedly, you'll find yourself running smack-dab into... classes.

(Functional) Disclaimers

Before you get up-in-arms, please understand: I'm perfectly aware that classes aren't in any way required in TS. I've found numerous cases in TS where classes seem to be the "default" answer - or where, by my estimation, they just seem to be the "solution of least resistance".

But this isn't meant to imply that classes are required for TS devs. If you're a FP fanboy, and a TS fanboy, you can pretty much write all of your glorious TS code without ever having to reach for that mean, ugly, nasty class keyword. But I can't help but notice that, over and over again, I start researching "how to solve Problem X in TypeScript", and over and over again, the suggestions I find from the community are written with classes.

Historical Context

I'm pretty sure that I understand how this (perceived) dichotomy arose. JS is TS. So, on some level, you'd think that we'd have the same trends influencing both communities. But TS has a very strong lineage... from C#.

Now, don't start hoopin' and hollerin'. I fully understand that there are few similarities (under the covers) between C# and TS. But, syntactically, TS looks incredibly familiar to anyone who's coming over from the C# world. And that makes perfect sense, because TS is the logical spawn of Microsoft's previous involvement in the ECMAScript open-source community.

With all that in-mind, it makes perfect sense that TS and C# share (syntactic) constructs such as:

  • Classes
  • Interfaces
  • Decorators
  • <>-syntax type-castings
  • etc., etc.

But I'm still sometimes baffled to see how classes are increasingly shunned in the "plain JS React" community - but they seem (to me) to be much more tolerated in the TS-React community.

An Anecdotal Contradiction

I was inspired to write this because I have a friend. He's a dev, like me. But he's far younger than me. And he's, frankly, a far better dev than me - a real "whiz kid" type. He lives-and-breaths this stuff. While my experience usually allows me to hold my own in a conversation with him, I'll freely admit that he's already internalized much more deep knowledge about programming than I ever have.

This friend of mine is a pretty hardcore "class hater". He thinks that the keyword never shoulda been introduced to JS. If the JS code works perfectly - but it uses classes - he sees it as a central flaw.

In the last year-or-so, he started making the TS conversion. He was slow to adopt it - but once he did, he really "found religion" so-to-speak. I don't honestly think he wants to code in anything but TypeScript at this point.

But something fantabulous happened during his conversion to the TypeScript (Dark) side: Somehow, he's magically lost his aversion to JavaScript classes.

To be clear, he still hates classes in vanilla JavaScript. And yet, it doesn't seem to bother him when he sees classes in TypeScript. I've even asked him about this directly - and quite frankly, as much as I respect his overall skills, the fact is that he's offered nothing but lame platitudes to explain his continuing aversion to JS classes, all while he has no problem with TS classes.

Confusion/Conclusion

So, at this point, I don't really know what to say. I realize that some TS devs hate classes. And I know that some JS devs are perfectly fine with them. And maybe the only "problem" is in my own perception. Maybe the trends are not what I see from my old, cranky perch on the programming shelf. But if there is anything to my analysis, it just doesn't seem to make any sense why "hardcore" JS devs are so disdainful of classes - and yet many "hardcore" TS devs don't seem to have any problem with them.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player