Rob Sutcliffe
Sutcliffe's Notes

Sutcliffe's Notes

8 Reasons To Use Typescript

8 Reasons To Use Typescript

And One Why Not To!

Rob Sutcliffe's photo
Rob Sutcliffe
·Jun 10, 2022·

5 min read

Play this article

Table of contents

  • 1. It's Really Popular
  • 2. Bragging Rights
  • 3. More Efficient Code
  • 4. Better Communication
  • 5. Find Errors Early
  • 6. Better Developer Experience
  • 7. You can Still Use Javascript
  • 8. It'll Stick Around For Awhile
  • -1. It's Adding Some Complexity

Typescript is created and maintained by Microsoft, well documented and is becoming really popular.

Although it's relatively new it's become one of the most in-demand programming languages. Not as much as Javascript, but considering all Typescript devs can also write javascript this is hardly surprising. Most of those coding polls that encourage people to learn a new language rather than improve the one you know suggest Typescript as a good option. On the plus side, you're not really becoming one of those people who switch languages, just when the learning curve ramps up, you're still improving your Javascript

2. Bragging Rights

Old school code snobs get annoyed if you call Javascript a programming language because it doesn't compile so it's a 'scripting language'. They miss the point that a scripting language is a type of programing language. And they miss the fact that Javascript almost always does compile (the ECMAScript spec doesn't state if it should compile or not anyway).

Well, with Typescript you can say, 'it is compiled' and 'it is statically typed' (snobs tend to complain about things not being 'strongly typed' but that doesn't actually mean anything. Languages are either statically typed or dynamically typed!)

Both of these facts keep code snobs happy... well nothing makes them happy, but it keeps them quiet at least. And they're right about a few things...

3. More Efficient Code

Because your code is been compiled, the compiler should optimise it better than you. But it'll also run more efficient at runtime anyway. You can really slow a site down if your function doesn't parse well.

During most browsers runtime compiling, the browser will optimise your functions, and it does this based on them always receiving arguments of the same types or same object structures. If you have a function and you sometimes send it a string and sometimes send it a number... Well, your browser is going to revert back to the un-optimised version of that function. Result? Much slower site! You might as well give them more images to load.

4. Better Communication

Typescript forces you to be explicit about your intent when writing code. When you or someone else looks at a function one month from now, you don't need to recall or communicate as much of your intent. It's saved right alongside your code. Naming functions is hard, but adding some information about what types they receive and return can add to an awkwardly named function. A variable named age could be a string or a number. But now it is declared as age: number... probably a number. Not much more to say about that.

5. Find Errors Early

Because Typescript is compiled into Javascript we get errors with types as we compile. Or if we have our IDE set up, we get them as we write them. This can leave extra room for errors tho. We may call an API or get data from any external places and Typescript simple won't know about it! We may still get some errors in our console but will be left searching around for what went wrong.

We could use something like propTypes which will check types at runtime and only run in developer mode. But this might be a bit too much work and too much to maintain. But using either is better than using neither at least.

6. Better Developer Experience

If you're using a decent IDE (seriously just pay for Webstorm, it's not that much) you'll find out your issue as you type them! If you haven't declared a type, if you've sent the wrong type, you'll find out as you write that line of code. This will slow you down loads when you first start! And it's frustrating enough you may want to give up. But instant feedback is the best and it's all saving time later on.

I get handy hints about how I can solve the problems too. But don't overdo it with the //@ts-ignore comments, might as well learn then and there how to fix it. Sadly sometimes you just need to ignore them and get the job done!

7. You can Still Use Javascript

You don't have to convert the whole project to Typescript. You can start incrementally improving things one file at a time. It can be daunting to 'upgrade' a whole. Start with something small and get it working well and it'll improve everything that interacts with that file. As more files use Typescript the advantages will be compounded as they can all benefit from the shared type checking.

This can be overlooked, but one reason not to start using typescript is that it's going to take ages. Just choose the file that is the most important or shared by the most devs change it to .ts and fix up those changes.

8. It'll Stick Around For Awhile

Javascript isn't going to add something like static types. It's backwards and forwards compatible so it's not going to make such a change directly. You can still load a website made 20 years ago in your browser and you'll be able to load it in another 20 years.

It also takes time to type check. We aren't going to do it in our production code, we don't even use propTypes in production. So Typescript won't be replaced by some new Javascript feature. It'll be a separate language and you'll be writing it separately and it'll probably continue to grow in popularity as large companies want to reduce errors and share code between more people.

-1. It's Adding Some Complexity

Let's be honest. Like with every new shiny thing. There is a learning curve, there is extra complexity. If you have a small team or you're working on a fast-changing start-up or even a prototype. Then why add that complexity. If the code probably won't be there in six months or if everyone works on the code base enough that they know most of the code.

And, dare I say it, with some applications you need to weigh up how important it is to even avoid errors. Sometimes you need to get something out that works well enough. Typescript is a no-brainer for Microsoft, but the chrome extension side project you made to add one feature to a website and shared it with 100 people, maybe not. You know if it's worth the effort for your project, I hope this helped you decide.

 
Share this