One day I came across a tweet from Lari Mazza that says

“Can I make a suggestion? Types are hard to understand when you’ve only worked with JS in your life and suddenly have to learn TypeScript”

As a software engineer that learned Python, Ruby, Javascript, and Clojure first, when I tried C++, it was a horror movie. I couldn’t do much, so counterproductive, and frustrating. Maybe because I was doing everything wrong and I didn’t understand types the right way.

But even though I had so many problems, I could implement a bunch of algorithms and data structures.

Now I’m using more and more Typescript in my day-to-today job and my side projects, I feel I’m more prepared to confront types. Actually, not confront, but use them in my favor.

This post is my attempt to help developers think more in types and understand this mental model.

JavaScript types

If you’re here, you probably heard that Typescript is a superset of Javascript. If not, great, you just learned something new today. YAY!

Typescript is a superset because any Javascript code is valid in Typescript, syntactically speaking. It may or may not compile depending on the Typescript compiler configuration. But in terms of syntax, it works just fine. This is why you can migrate Javascript to Typescript progressively by just replacing the .js extension with the .ts. Everything will be without type declarations (the any type), but that’s another story.

Also, if you code in Javascript — or any other programming language — you probably think in types:

  • “Hm, it is a list of integers, so I’ll need to filter only the even numbers and return a new list”
  • “This is an object, but I just need to get this string value from the property X”
  • “This function receives two parameters. Both A and B are integers and I want to sum them”

Yeah, you got the idea. We think in types. But they are just in our heads. We constantly think about them because we need to know how to handle, parse, or modify data. We need to know which methods we are allowed to use in this object type.

To give a more concrete example, imagine you want to sum the price of all products. A product object looks like this:

const product = {
  title: 'Some product',
  price: 100.00,
};

But now with a list of products:

const products = [
  {
    title: 'Product 1',
    price: 100.00,
  },
  {
    title: 'Product 2',
    price: 25.00,
  },
  {
    title: 'Product 3',
    price: 300.00,
  }
];

Ok! Now we want a function to sum all the products prices.

function sumAllPrices(products) {
  return products.reduce((sum, product) => sum + product.price, 0);
};

sumAllPrices(products); // 425

Just receive the products as the argument and reduce all product prices. Javascript works just fine. But while building this function you start to think about the data and how to handle it properly.

The first part: products as an argument. Here you just think: “well, we’re receiving a list of some objects”. Yeah, in our heads the products are a list. This is why we can think of using the reduce method. It is a method from the Array prototype.

Then we can think about the object in detail. We know that the product object has a price property. And this property is a number. This is why we can do product.price and sum with the accumulator.

Recapping:

  • products is a list of objects.
  • As a list, we can use the reduce method, as this method a member of the Array prototype.
  • The produce object has some properties. One of them is the price, which is a number.
  • As a number property, we can use it to sum with the reduce accumulator.
  • We wanted to return a number, the sum of all product prices.

We are always thinking of data types, we just need to add the type annotations to make it more explicit and ask the compiler for help. Our memory is limited and the compilers are here to help us, humans.

The type system will not only make our data more consistent, but it can also provide autocompletion for data types. It knows the types, so it can show the members for the data. We will take a look at this idea later. Here I just wanted to show that we think in types in our heads.

#typescript #web-development #javascript #programming #software-development

TypeScript Types Explained — A Mental Model to Help You Think in Types
1.20 GEEK