Why TypeScript?

This weekend I watched a talk about TypeScript at Nebraska.Code().  The presenter seemed to have a good grasp of TypeScript and was a good presenter.  Sadly, that combination doesn’t always happen.

One thing I was glad about, was that he put up a picture like this:

I immediately thought, “This is going to be good!” as I figuratively rolled my eyes. But instead, he said “This is NOT the problem with JavaScript.”  That comment immediately made me realize that this was going to be a talk that wasn’t going to just bash JavaScript.

And to be fair, he did.  He identified the following problems with JavaScript:

  • No type safety
  • Functional language with inconvenient syntax
  • Object base, not object oriented
  • Few organizational constructs

He then offered the following reasons as to how TypeScript helps:

  • Optional static typing
  • Familiar OO constructs
  • Enhanced functional experience
  • Improved code organization
  • Works w/ existing JS libraries

The impression that I left the talk with was “TypeScript makes JavaScript object oriented.”  The speaker never said this, and I don’t think he even tried to imply it, so it might just be a combination of everything else I’ve heard about TypeScript coming to a head.

After watching this talk, I’m still not convinced that TypeScript is the way to go.  I like ObjectOriented programming. The bulk of my career has been using OO principles (C++ and C#.)  But I’m of the mindset that not every language has to be object oriented. After all, C is not object oriented and it definitely serves a purpose.  That doesn’t make C a bad language. Just as Haskell isn’t a bad language. Just because they are not object oriented. Are they possibly harder to learn? Maybe, or maybe they’re harder to learn only because we’re so used to OOP.

I’m reminded of when I was in seminary. I took 15 hours of Greek and 15 hours of Hebrew. I started with the Greek classes, and they were way easier than Hebrew was.  I had a smart friend who started with Hebrew and he really struggled with Greek. To me, it was strange, because Greek was so much easier to me.  The problem was, he was coming from a different paradigm, a good grasp of Hebrew, to a new paradigm, koine Greek.

I think the same thing is true with programming languages. JavaScript has some problems (tell me a language that doesn’t.) But I don’t think one of it’s major problems is that it’s not OO. I’m also not convinced that it having dynamic typing is necessarily a bad thing either.  However, approaching JavaScript as if it WAS an OO language or that it HAD static typing is going to cause you problems.  In the same way as taking an English sentence and translating it word for word into Spanish is going to sound off or “wrong” to Spanish speakers (if for no other reason that the differences in word order.)

The best example, to me, from the talk about why TypeScript is beneficial was the example of inheritance. For this example, he demonstrated a relationship like “A developer is a type of employee.”  For us OOP folks, we immediately get that. But it’s different in JavaScript because it’s a prototypical inheritance. TypeScript cleaned up that type of inheritance with no problem. The TS code was much smaller and easier to read than the generated JS code. The speaker even commented that in 6 months, when he has to come back and make a change, he’d rather make a change on the TS code than the JS code.  The JS code had 3 or 4 levels of nested functions.  By and large, I would agree. If those were my only two options.  But I feel like that type of solution is missing something.

If someone came to me and described a problem domain with highly related entities, for example, the classical Animal:Mammal:Human hierarchy that is used so often in OOP, my first thought would not be “I know, let’s use JavaScript to model this.”  In the same way that I would not think “I know, let’s use MongoDB to store it.” I guess what I’m saying is, while TS seems to clean up inheritance, a possibly better way(?) would be to use a different language.

And I think that is my biggest struggle with TS. I get that they’re trying to make JS better, much like C++ tried to make C better (and arguably that C# tried to make C++ better.) But the difference to me is that C++ doesn’t compile to C. It’s a different language that generates its own binaries.  I think if TypeScript had native browser support like JavaScript, I would be more prone to adopting it.  But even then, only if it was its own independent language.

Those are just my thoughts. I’ll still keep reading & listening to stuff about TypeScript. I’m sure at some point, I’ll try to write a Hello World type of application. But it’s still further down the list of things to learn for me.


Leave a Reply

Your email address will not be published. Required fields are marked *