corysimmons / blog

Using Github Issues as a blogging platform.
0 stars 0 forks source link

Why Typescript? #7

Open corysimmons opened 4 years ago

corysimmons commented 4 years ago

image

Is it not a strange fate that we should suffer so much fear and doubt for so small a thing?

Typescript has been around since 2012, but it wasn't until a few years ago that it really started picking up speed. A lot of famous JavaScript library developers started switching to it (particularly in the Angular, and then React, communities—and now everywhere). This was fine to ignore at the time because it didn't affect me. But in the past few months I keep noticing this trend in job descriptions: "extensive Typescript experience".

I'm not new to types. I wrote a pretty large open-source project using Flow (Facebook's challenger to Typescript), and I remember thinking Typescript was better for a few reasons:

  1. In Flow you can cheat and just ignore typing half of your project (which doesn't make it very reliable). You can do this in Typescript too, but it requires the malicious effort of going through and adding the any type to everything, whereas you can just "forget" to type certain files in a project with Flow. So Flow's biggest selling point is actually a weakness.
  2. Typescript has a massive collection of types for various popular npm packages out there. For instance, React. Go to https://microsoft.github.io/TypeSearch/ and search for some of your favorite libraries.

But what is Typescript?

In programming, there are two types of languages: loosely typed and strongly typed.

JavaScript is loosely typed. If I say var x = 1 then I (or some library) can accidentally re-assign that x var doing something like var x = "1" later on.

So think about that for a second. You are expecting x to be a number of 1 but it got changed to a string. This can frequently introduce bugs. Like... you are lying, or new, if you haven't ran into a bug like this hundreds of times in the past.

Just for type safety, I wouldn't say introducing Typescript is worth the overhead. I don't run into type bugs very often so it's not a huge selling point for me. The real benefit of using Typescript is all the annotations your code editor gives you now. Holy crap your code editor will light up like a Christmas Tree.

So I create an type interface (which is just something that looks like a JS object) and then feed that into a React component, and 💥—if my component doesn't have the right props passed into it, then VSCode let's me know.

Untitled

Now I never have to worry about if I forgot a prop that my component needs.

But wait, there's more!

These type definitions also act as pretty damn good layer of automated documentation.

Untitled 1

I'm having a hard time holding option and hovering that IPost while simultaneously doing the keyboard shortcut and dragging to get a screenshot, BUT IF I COULD, it would show you a popup with:

export interface IPost {
  title: string
  body: string
  author: string
}

Think about how handy that is. 90% of the documentations we use are API References, and this is effectively that.

Think about that co-worker who writes all kinds of spaghetti and never documents it. Now you'll know the required (and optional) parameters, and types, of every function they push to the project. 🔥

Does this mean docs are dead?

There's still a place for a greeting page with a getting started and some demos, but for the most part, yes, people should stop writing docs. Traditional docs are frequently outdated/broken and frequently a huge source of frustration for me.

Typescript can't lie or forget to update docs, so, combined with decently-named functions and parameters, it's strictly better.

It looks hard.

It is and isn't. It's intimidating at first, so I highly suggest you make a ☕️ then speed-watch a video course on it to at least have a compass.

Then it takes a small project to get used to, but after that you'll be competent enough to fall in love with it like so many have before you.

You really don't have a choice. Like I said, Typescript is more-and-more frequently appearing in job descriptions, and being used in every significant project on GitHub. Unless you're the CEO/CTO, it's only a matter of time before you're asked to learn it. Might as well start now. You'll like it. 😇

Stay safe. ❤️