8 Reasons To Use Typescript
And One Why Not To!
5 min read
1. It's Really Popular
Typescript is created and maintained by Microsoft, well documented and is becoming really popular.
2. Bragging Rights
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
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!
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
-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.