May 2, 2022

Learning TypeScript by Building Stuff

For the longest time, I resisted learning TypeScript. For some reason, coding in a language that's not a language but compiles to a language seemed like overkill to me. It maybe because I had gotten so used to JavaScript. I was comfortable with JS, maybe too much.

I had a former colleague who tried to persuade me into learning/using it but I never took the plunge. Late last year, I was about to start a new Next.js project, when I had this question in my head:

Should I make this a TypeScript project?

At this point, I've seen some TypeScript code but I had zero knowledge about the it. Since TypeScript was mostly JavaScript, I opted to code my new project in TypeScript.

The first step was to get myself acquainted with the basics. It was a simple matter of reading a couple of blog posts and watching YouTube videos. Initially, I didn't write any TypeScript code but used TypeScript file extensions, .ts and .tsx.

Then I started using the built-in types wherever possible. I even used types exposed by Next.js and React. This is where the trouble started. My IDE of choice, Webstorm, has excellent support for TypeScript. As I started using types from Next.js and React, the errors started to creep.

It was annoying. At first. The sheer number of red squiggly lines in my editor and ESLint's refusal to push code because of those errors was infuriating. But I realized that I was coming at it with the wrong mindset. Those red squiggly lines were learning opportunities, each one was a lesson. I also reminded myself that each error could be a bug waiting to happen.

So I started spending extra time resolving those errors. At least, most of them. Sometimes I give up trying and use the // @ts-ignore comment. I could force ESLint to not allow the cop out comment but sometimes I wanted to build things fast. I could always come back and fix the issue (yeah right!).

Then I started adding custom types/interfaces, mostly simple ones. It's extra work but doing that work ahead of time seemed worth the effort. I now understood why my former colleague loved TypeScript. I understood why TypeScript devs swear by it.

Slowly but surely, I started loving TypeScript. I like how it forces you to think about your data types. I like how it brings structure to the data you pass to your functions and components. I like how I don't have to guess what type of data I have to pass. I like how flexible it is and how it's watching my back and preventing me from making mistakes that are easily overlooked.

I'm not an expert still and I have a long way to go. To be honest, I don't want to be an expert. I want to learn enough to get the job done and when the job requires me to know more, I'll learn just enough to get it done.

If you're a JavaScript dev who hasn't spent the time to learn TypeScript, I implore you to give it a shot. TypeScript is everywhere nowadays. Most NPM packages that we use and love are written in TypeScript.

There's no one right way to learn anything, let alone TypeScript. Getting the basics down won't take long. Read blog posts, watch YouTube videos, solve problems on Exercism, do a short course. Or do what I did and start a TypeScript project and then learn as you go.

Share this on:Twitter