Learning TypeScript (Finally)

Learning TypeScript (Finally)

No look I get it, I know I know, I'm late on this one.

Typescript has been around for ages now and I'm just now learning it. As always, I'm behind the curve on a lot of this stuff. I'm working on it, okay?

This post is going to be all about why I'm learning Typescript, what Typescript is and why it's become A Big ThingTM in the web development world.

Wait what the hell is Typescript?

Yeah, that's fair, I'm with you.

Typescript is a superset of Javascript, meaning that legitimate Typescript will run just fine as Javascript... but it isn't Javascript. It needs to be compiled down to Javascript using the Typescript compiler.

Why would you add a compilation step to writing Javascript, then?

Well, Typescript enforces static typing. I at first underestimated the importance of this fact when I first heard about Typescript until I really got into the weeds of writing Javascript code bases, which is when I realized just how frequently I was running into run-time bugs caused by loose typing. Loosely-typed languages like Javascript and Python are great because you save time typing out the explicit typing, and you save time worrying about type conversions, casting, etc., but they can and very frequently do lead to more bugs at run-time when edge cases give you return or variable types that you weren't logically expecting and your programming language is loosely-typed and doesn't catch it.

Think of it this way: I have a function that reaches out to a web server, grabs some JSON, pulls out a field and puts it in a variable that is then returned to your main function, which concatenates that string (using the "+" operator) with another string and prints it. This works fine for a while, until, for whatever reason, the web server decides to return an integer in that JSON field. Now, since you're using a loosely-typed language, that error is going to get passed downstream when your code now is trying to add an integer ot a string instead of concatenating. A strictly-typed language will catch it when you try to assign an integer to a string-type variable, which will make the problem much easier to find and debug.

It might seem strange and nit-picky to you like it did to me in the beginning. I get it. Trust me, though, Typescript solves some fairly important problems in Javascript coding, to the point that I'm planning on wholesale converting all of my projects over once I learn it well enough.

Why am I learning Typescript?

Honestly this is a fairly uninteresting answer: I have a project I'm working on that requires it. 🤷♀

I also know that it's an important asset for a full-stack web developer to learn, or at least to be familiar with. If there's a new tech stack, new buzz word or a new library, it's always good to at least initially check it out and be vaguely familiar with it, in my opinion, though this is something that I'm very obviously behind on. It's why I'm playing catch-up now, basically to catch up with the curve on a lot of technologies and environments so that I can stay up-to-par at the very least with other full-stack developers.

Typescript also just seems like an objectively useful thing to learn as well, for reasons I'll go into below.

Why should you learn Typescript?

Typescript seems like a better way to write Javascript, honestly.

I say this as someone who pretty much exclusively has written in loosely-typed languages, they cause more bugs. Expecting a string variable returned with no explicit typing makes it fairly easy to accidentally code in surprise edge cases where an integer is returned, which can bork things down the road when you're trying to concatenate the returned string with another string but instead you end up trying to add a string to an integer.

This is obviously a fairly contrived example but that's the general argument for forcing type-strictness in Javascript. It makes your code less bug-prone in theory, at the cost of a bit of convenience and development time. The cost in development time is theoretically outweighed by the saved costs in finding and fixing bugs at run-time, and the compile time is seemingly fairly minimal, I would think. If your development cycles are upended by compile times, you probably scheduled on too tight a deadline anyways...