Typescript in the real world — Why and how you should use it

Yann s
Engineered @ Publicis Sapient
14 min readAug 7, 2019

--

Introduction

Typescript is an exciting technology that has been around since 2012. It has been implemented by Microsoft, which constantly updates and improves the language and its tooling. Over the past few years, it has been used by many people and companies, making it one of the top trending languages on Github.

This documentation’s purpose is to expose a bit of why you should consider using it, and a lot of how you can use it. The main reason for this guide to exist, is that most guides around the web simply assume that you are already on the front seat of the hype train for Typescript. This guide assumes that you see Typescript like a father sees the new tattooed boyfriend of his daughter.

In the real world, Typescript is being used in a lot of large projects: VS Code is written in Typescript, Angular is written with and in Typescript, as well as Formik and RxJS, to name a few.

Why

Before jumping to the good stuff, I guess it’s fair to add a small extract on why you would want to make your life harder. Typescript appears to be a burden to the neophyte eyes, nonetheless, I’m going to try my hardest to show how easy it can actually be to use it, and to enjoy it, without the hassle of suddenly having to write Java code.

Typescript reduces the number of unit tests you need to write

One of the first reasons I see is the insane gain of time when it comes to testing your code. If you wrote any tests at all before, you can count on a 10–20% decrease on how many you have to write. If you didn’t, then you gain those extra 20% at no cost!

This simple function already highlights the second problem I was going to expose: JS *does* have type already whether you like it or not. But for now, my point onward: in your unit test, you need to make sure your function throws when arguments are null, undefined, strings, etc… Another test is to make sure the function always returns a proper number and not (for example) an empty value.

--

--