If you start a new project or create a new file in an existing repository using plain Javascript, you're already introducing technical debt to the project because the default is to work with Typescript instead.
At this point, you might disagree with me but the idea of this post it’s to help you surf the Typescript wave! 🌊🏄♂️
Typescript’s like a tsunami that’s powering most of the new projects and companies are even migrating existing projects.
But you might be wondering why everyone it’s using it. So the answer is popularity and all benefits of being strongly typed.
In our community, once something is popular it becomes the default, what everyone uses, what’s asked during interview questions, and so on.
Another concrete benefit is strongly typed, but if you haven’t used it in another programming language you might be skeptical at this point. So from now on, I’d tackle each point that you could benefit from being strongly typed.
Explicitly
There is a phrase in Python’s Zen:
Explicitly is better than implicit
Let’s look at the following snippet
Just by reading the code, you can’t determine what each movie has. This means you don’t have enough context and eventually would need to exercise the endpoint to know what’s available or not.
On the other hand, let’s look at a Typescript example:
It’s clear what movies are and what properties can be used.
Intellisense
Following the previous example, now the editors have more hints to let us know what properties to use which would speed up our coding and minimize the errors we could introduce by silly things like mispellng
Spot errors during development
It’s quite common to introduce code refactors, change code around, or add new functionality.
These are things we are used to doing daily and is likely that at some point we do something wrong, and could introduce a bug that lands into production 🫢
If you are unfamiliar with the following image, let me present the cost of finding a bug in the different stages.
So, it’s clear that the sooner we spot errors the better and that’s a key advantage of using Typescript over plain Javascript.
Typescript would give you feedback as you write code like the following example:
Like any tool, if you don’t use it properly it just becomes useless. For that reason in the following section, I’d share with you common mistakes I’ve seen during the two years of writing Typescript at my current company
Ignoring errors
The easiest way to tackle a typescript error is just to ignore it. But soon enough it could become the norm and you see comments like the following spread all over the code base.
//@ts-ignore
Solution: be strict with its usage & request the developers to properly handle them. If they can’t fix it, probably it’s a lack of knowledge rather than it being impossible to fix.
Overuse of “any”
Any is the same as having no typing in your code, so it becomes useless to have Typescript if your source code is full of any.
Solutions:
Set the “noImplicitAny” true in the typescript configuration
Encourage the team to NOT use any since it’s a bad practice.
Repeated types
We are all familiar with the DRY principle but seems like people tend to forget about it or they are just lazy to either look for places where a type could already exist or refactor the code to elevate a type to a common folder/place.
For example, let’s say you develop a user table and define a PaginationType, then another developer works on another table somewhere else and also defines a PaginationType.
At this point, we have the same type in different places, we aren’t following the DRY principle and eventually could create inconsistencies.Solution:
There is no recipe for this particular problem but one thing you could do it’s to define global types where generic things would live, like types for the user, pagination, filters, etc.
To recap, definitely Typescript is something you are currently using or soon enough you’d be working on a project where you have to use it.
If you have some experience with strongly typed programming languages, you know the drill, otherwise, I’d recommend you dive deep into the Typescript Documentation gain experience and learn how to use it appropriately.