DEV Community

Cover image for TypeScript and why you should type your JS project
MiguelDevelopez
MiguelDevelopez

Posted on • Updated on

TypeScript and why you should type your JS project

TypeScript is like JavaScript but with no surprises.

I heard a long time ago about TS, great stories about how useful it is, allowing js to have Javascript types. At this moment I wasn’t so 100% aware of the sense of type everything, now that I have more knowledge about clean code, good practices and start to develop some little applications in React I think that the code could be better with types and I decided to investigate about and… this is what I found:

🥰DEVELOPERS LOVE IT

Here are some charts about how people start to use more and more…
image
More interesting charts about JS here: https://2020.stateofjs.com/en-US/technologies/javascript-flavors/
Increasing the usage in GitHub projects.
image
https://octoverse.github.com/

And high place in the 2020 developer survey of StackOverflow:
image
https://insights.stackoverflow.com/survey/2020#technology-most-loved-dreaded-and-wanted-languages-loved
Big companies like Airbnb support its use and claim that using it would significantly reduce potential errors.
image
Source: https://twitter.com/swyx/status/1093670844495089664/photo/2

I went deep into Reddit to find some real words about it and the same: “It's very hard to go back to Javascript once you start writing Typescript”.

🚄But… WHY ALL THIS HYPE?

With so many people loving it I decided to learn the basics and this is what I have learned that it can offer.

Advantages:

  • SUPERSET of JS, almost the same language but with consistency in types.
  • The main advantage is strict typing, you can type everything, from variables to the parameters of a function, and even the structure of an object, that's can prevent about 15% of the bugs that end up in committed code.
  • The ability to find these obvious yet frequently occurring errors this early makes it a lot easier to manage your code with types.
  • Types make code management easier and more predictable.
  • In TypeScript we can define interfaces for parts of our program, so we can be sure that they interact correctly. It means they will have clear contracts of communication with each other which will significantly reduce the number of bugs. TS + unit tests will do the code more stable, predictable and it will reduce the amount of pre-release bug density.

But… Not all that glitters is gold...

Some little disadvantages:

  • Apply type may make you slower in the first instance, in the long term it is better but you have to get used to it.
  • Required compilation.
  • And of course, TypeScript does not catch run-time type errors. It means that you can write the code that will pass the type check, but you will get an error upon execution.

🤖 Examples of SYNTAX

Basic typing when you declare variables, you won't be able to change the type later (even if you don't declare strictly the type it will be typed), making your code more reliable:
image
image
image
image
If for any reason, you need a variable without specifically type you can use any:
image
But... in the documentation, they recommend not to use it unless you are in the process of migration from JS to TS.
image
Add enum to JS, a way of giving more friendly names to sets of numeric values:
image
Typing parameters of a function will allow you to detect quickly if you insert something wrong
image
image
You can add what is the type of function that will return
image
image
but if you don’t do it typescript will do it for you
image
You can create your own types that allow you to don't repeat the code.
image
You can type also what you select from the DOM, allowing you access to all the methods of one kind of input.
image
There are also interfaces, a contract that should be matched with the object if you don’t want errors, and other
image
And of course, you can implement TypeScript into your favorite JS framework, I'll show you an example in a React project, providing the same advantages mentioned above:
image
Similar to the PropTypes and now you will have to add the props as mandatory when you use them or the IDE will warn you:
image
And when you introduce the mandatory prop person it will be okay:
image
You will be able to type other things (practically everything) like, for example, the hook useState:
image

And that’s it, folks, I hope you enjoy this little piece of info about TypeScript and maybe consider applying it in some project.

Here are some sources and interesting videos to check and go deeper if you want:
JSConf - Airbnb tactics and strategy to migrate to TS (very interesting)
https://youtu.be/P-J9Eg7hJwE

To type or not to type: quantifying detectable bugs in JavaScript
https://blog.acolyer.org/2017/09/19/to-type-or-not-to-type-quantifying-detectable-bugs-in-javascript/

Official documentation - TS in 5min to JS developer:
https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

TypeScript tutorial series:
https://www.youtube.com/playlist?list=PL4cUxeGkcC9gUgr39Q_yD6v-bSyMwKPUI
In this video, you can see how to apply TypeScript in React:
https://www.youtube.com/watch?v=Z5iWr6Srsj8

Oldest comments (58)

Collapse
 
hardikchopra profile image
Hardik Chopra

Great Article ✨ , a must read!

Collapse
 
migueldevelopez profile image
MiguelDevelopez

Thanks! 🙌

Collapse
 
ssijak profile image
Saša Šijak

Then you just dont have experience of working on large enough plain javascript object. Jumping into such project and changing anything is a scary proposition.

Collapse
 
ssijak profile image
Saša Šijak

Then you just don't have experience of working on large enough plain javascript object. Jumping into such project and changing anything is a scary proposition.

Collapse
 
peppesilletti_4 profile image
Giuseppe Silletti

The only thing that will back you up when making changes is having in place some nice, solid automated tests.

Collapse
 
johnkazer profile image
John Kazer

I think I'm just gonna make the jump from using jsDocs for typing (no compilation so not as robust I guess but somehow nicer) to ClojureScript.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

It is also possible to use Babel, tscomb or Flow.

Collapse
 
matjones profile image
Mat Jones

TypeScript is like JavaScript but with no surprises.

Sorry but this is just utterly false for so many reasons.

Collapse
 
johnkazer profile image
John Kazer

Could you list a few?

Collapse
 
matjones profile image
Mat Jones

Incorrect types for open source packages, happens quite often

If you’re targeting the browser... Internet Explorer

Generics not working the way you expect them to in certain scenarios

There’s lots. It’s also just a superset of JavaScript, so any surprise JavaScript can produce, typescript can also product.

Thread Thread
 
migueldevelopez profile image
MiguelDevelopez

Thanks for sharing these points, Mat!
I'll investigate the cons of using TS 🤔

Thread Thread
 
matjones profile image
Mat Jones

Don’t get me wrong, TS is a VAST improvement over JS, but it does have its own set of “gotchas”

Thread Thread
 
migueldevelopez profile image
MiguelDevelopez

To be honest, I don't use TypeScript yet, I just found it very interesting and loved by the community so... for that reasons I decided to write this, but I have in mind to start a little project to check his good points (and the bad ones too) 👏

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

That's true. But then TypeScript is getting better.

The latest change is ability to make array and record indexing return undefined. (by settings in tsconfig.)

But yes, I will never trust TypeScript to be truly safe, nor truly strongly typed.

Collapse
 
migueldevelopez profile image
MiguelDevelopez

and if you combine types and tests, will make the code reliable?

Thread Thread
 
matjones profile image
Mat Jones

If you have reliable enough tests, sure. And to be clear, I still think TS is a huge improvement over JS.

Thread Thread
 
johnkazer profile image
John Kazer

Having types of some sort helps me greatly in just writing functions, because it makes it much easier to remember just what data I'm messing around with. But I do tend to a functional/data-first style. So it reduces cognitive load way before you get to using tests/compiler to check things. And so, in fact, if your types are wrong your function/data manipulation will go wrong when you write it and a compiler/running tests are secondary things.

Collapse
 
matjones profile image
Mat Jones

Yeah, don’t get me wrong, TS is still a VAST improvement over JS.

Collapse
 
jwp profile image
John Peters

I love Typescript mostly for Typedefs which cover 95% of all JavaScript libraries. Immediate API discovery as I type.

Collapse
 
akashkava profile image
Akash Kava

ts-ignore is superior to as any

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

You can ban both of these with latest ESLint.

Collapse
 
akashkava profile image
Akash Kava

eslint-disable is superior to ESLint, its really difficult to catch unless you setup automated git monitor to track commit and warn usage of such features.

Thread Thread
 
patarapolw profile image
Pacharapol Withayasakpunt

Nonetheless, the idea of you CANNOT disable Golint sucks.

Collapse
 
migueldevelopez profile image
MiguelDevelopez

You diabolical...

Collapse
 
mzaini30 profile image
Zen

I think, I can't use TS in Svelte

Collapse
 
migueldevelopez profile image
MiguelDevelopez

It seems since July 2020 Svelte supports TypeScript 👌
svelte.dev/blog/svelte-and-typescript

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I use TypeScript (and Prettier) instead of ESLint and Babel, actually. And tsconfig is quite simple and intuitive.

Though, of course, you can use all of these together.

I wouldn't use uncompiled vanilla JS, as it is inconsistent between browser and node (as well as unminified and unfeatured).

So, if compiled anyway, why not TypeScript?

Collapse
 
stojakovic99 profile image
Nikola Stojaković

You can set up ESLint to catch cases like this - then it will warn you to add return type to a function.

 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

It doesn't matter if I don't fully benefit from it, does it?

Don't be too serious and try to fight JavaScript.

Furthermore, I don't have a problem creating my .d.ts.

Collapse
 
vbanditv profile image
Fima Taf

Wow, you just said everything that i think about typescript especially the comparison with kotlin.
I thought i was alone in this opinion but you prooved me the oposite, thank you!

Collapse
 
matthewbdaly profile image
Matthew Daly • Edited

OK, I'll bite.

Flow offers many of the same advantages as Typescript. You can specify the shape of objects, define common interfaces, and so on, and Flow will pick up on type-related errors for you.

I'd also argue that using a language that extends Javascript is potentially risky. You can always bet on Javascript being available in the future, but languages that compile to Javascript have a rather shaky history (can't be too much support for Coffeescript left, for example). Plus, Microsoft's track record on embrace, extend and extinguish is something to be wary of. By contrast, Flow was always implemented as just a set of annotations on top of Javascript that can be stripped out by a simple Babel transform.

For those reasons I opted for Flow over Typescript, and I have no plans to change anytime soon. I'm of the opinion that it's a better solution to the same problem, and provides similar benefits.

I will say that I regard both Flow and Typescript as transitional solutions - adding equivalent functionality to the Javascript language would be a far, far better solution.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.