Class Contradictions in TypeScript vs JavaScript
Tags: typescript, javascript, react, oop
Published: 2020-06-18 03:36:43

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.<br/>

1. 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_.<br/>

1. 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.<br/>

1. The previous point is amplified if you're an Angular dev.  Modern Angular has become, basically, a TypeScript library.<br/>

1. 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`_.<br/>
&nbsp;

> Wait... _What???_

&nbsp;
<br/>

## 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.
<br/>

## 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.
<br/>

## (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_.
<br/>

## 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.
<br/>

## 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.
<br/>

## 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.