Over the last few weeks I’ve been diving into learning TypeScript from the ground up. It’s been a steep learning curve and sometimes it feels as hard as learning JavaScript all over again. It's very satisfying though to start feeling confident when working with types. These are my notes from learning TypeScript.

An image of a Macbook Pro Photo by Andras Vas on Unsplash

What is TypeScript?

TypeScript is a strongly typed language that builds on JavaScript. TypeScript allows developers to define types for variables, function parameters, and return values and it reports errors when these types don’t match, TypeScript code is compiled into JavaScript before it runs in the browser or with Node.

Why use it?

The main benefit of using TypeScript is its capacity for error reduction. TypeScript’s static type-checking helps catch errors during development rather than at runtime. This reduces the likelihood of bugs, especially in larger codebases. In fact, it eliminates a class of bug (such and such a variable is undefined) entirely. Enforcing types mean that you as the developer will not be allowed to do something that you previously told the compiler you didn’t want to be able to do.

How I’m learning it

I’m learning TypeScript through an online platform called Execute Programme. This gives me short interactive lessons where I can test what I’m learning by writing and running real code. The thing I love most about Execute Programme though is that it prompts me to review previous lessons at increasing intervals. This really helps me to remember what I learn and is definitely helping me to solidify some of the concepts behind TypeScript.

Challenges

I have struggled with TypeScript frequently since starting to study it. The topics listed below have been the most puzzling for me so far but I’m sure there’s more to come!

  1. Type unions and intersections
    I am only just starting to grasp how these work and the difference between them. I couldn’t understand why when a variable could be either a number or a string you couldn’t then assign it to a number. I found this video that explains unions and intersections using set theory to be really helpful for this

  2. Distinction between types and run time values
    Part of why I struggled with unions and intersections was that it took me a while to understand that TypeScript doesn’t have access to run time values. This makes sense because types don’t exist at run time when TypeScript is compiled to regular JavaScript.

  3. Index signatures
    I’ve come across these recently and I’ve been caught out by type mismatches between an explicit property and an index signature. I’m learning that Index signatures can be thought of as an overall type for an object and individual fields need to conform to that.

  4. I’m looking forward to looking back on this post in a few months when hopefully these things will be easy for me.

    Future plans with TypeScript

    Now that I feel I’m comfortable with the TypeScript basics course I’m hoping to finish the Everyday TypeScript and Advanced TypeScript courses on Execute Programme. I would like to convert my domestic abuse services mapping tool from JavaScript to TypeScript as well. Finally, I’m going to put my skills into practice by writing and reading more TypeScript code in my work at Common Knowledge.