One thing that made me love typescript!

Photo by Robert Bye on Unsplash

One thing that made me love typescript!

I love Javascript. It's so plain and easy to build something beautiful without having to tackle the hassles of type errors. I mean, just think about, you create a function which takes an argument and returns a value. You give a damn about what types of argument goes in there, you just trust that whatever goes in there is what you've always wanted. It's all about trusting... blindly.

   function doSomething(arg){
          .... do some manipulation
         return value;
  }

Okay not blindly may be, you add several "throw catch blocks" or "if" statements to guard the type of arguments that enter on the function. And it's so easy.

When you think about it.. typescript isn't so hard either, you just add what "type" of argument goes in there. How hard can it be?

  function doSomething(arg:number):number{
        ... do some manipulation
        return value;
  }

Easy right? Of course it's just a simple example. But if you go deeper, somewhere along you will have some complex typing with unions, type guarding, and other unnecessarily complex type structure that you've gotta maintain just to pass a simple data which you know will always have the type you want. ( If you're a typescript developer, then you know the pain). It's just gets so frustrating and at times you want "any" to rescue you. It's annoying, I've been there.

Despite all that efforts and complexities, I've grown to love typescript and what it brings to developer experience. And now, it feels so messy and unstructured to code in Javascript and believe me it's just so much easier to build something fast with javascript.

But when you're working on some big projects or projects that changes frequently, typescript is purely a blessing.

The first time I started using typescript was on a project which , at the time, was just a POC (proof of concept) . The design concepts, functions and API specifications were frequently changing.

I have worked on something like this before using javascript. The client was not sure what he wanted and demanded changes almost daily. You do something, create a functions implement those in your modules and get it running after lots of debugging. You are at peace and then the client picks up a simple (from his perspective) change and of course you finish it hastily. How hard can it be, right? A small change. A couple of minutes to half hour maybe. The client is happy, you are okay. All good. But then after a while, the client sees that another module has crashed and he complains. What happened? Just a minor problem with the fix you wrote for the changes the client asked. Of course, you didn't notice the coupling between the modules and how the changes on one part of the code was affecting another module but it did reflect on run time. You start losing your credibility over your own code and you start questioning whether the changes that you did affected another part of your project.

The ripples of changes were hard to keep track of. The project was always difficult to maintain with the frequent changes demanded by client. With this hardship already experienced, I was ready to work on the POC and was of course expecting disappointments. Yeah, it was very hard to maintain the typing and sometimes those were just unnecessary and very very complex. I had to create types for all the responses to be received from the API, for all the data to be sent, for all small functions and for entire insignificant variables. My code was fully typed. Then, the responses of API changed on another sprint.

This is when I started to fall in love with typescript. I just changed the type of responses coming from API and amazingly all the codes depending on the response of API went red. I know as a developer how irritating those little red underlines on codes can be. But trust me, when I saw how the Vs code knew where to draw those red lines, that felt so satisfying. Without even running my codes, I just scanned for those errors and knew the ripples that spread through my project due to my small changes in another part of the code. I could just fix those problems and trust that it works without throwing any further errors.

Of course, there are several other benefits to typescript that you might have read or come across. But for me, I have always loved how typescript shows the data coupling on my project and warns me beforehand.