DEV Community

Discussion on: TypeScript?.. or JavaScript?

Collapse
 
dotintegral profile image
Artur Siery • Edited

I feel the opposite. Recently I was working on a big project with advanced FP patterns. We composed and composed a huge amount of functions that were processing our monads. Sometimes, despite trying to keep everything simple, it's easy to get lost in big functions compositions. Especially when you enter a file for the first time and just want to make a small change, but you're unsure what's the structure at given point. Then comes TS and helps you with understanding quickly what's going on.

Though TS is not perfect and will not reduce run-time bugs (that's why we used monads) it can be helpful and improve your dev experience. The problem with TS is that you need to understand it's strong and weak parts to use it efficiently. It's the exact same thing as with JavaScript - it gives you a huge amount of tools, but it doesn't mean you should be using all of them. TS can bring you value, but you need to have a bit experience. Perhaps that is why a lot people are saying TS is crap. Because they haven't got to learn the good and bad parts and quit in the middle.

In TS, I don't really care about the types themselves. They are just a first step for what it brings. It helps you understand what are the structure of the objects and what are the arguments and return values of your functions. This is where it is most effective. For example, when working with pretty nested object - like the responses you get from hedless CMSes like Contentful - it's really easy to make a type-o or to refer to object.name instead object.title This is where TS comes and makes your experience much more bearable.

In addition to that, if configured properly (that is with strict mode on) it can warn you that when calling object.something() your object can be nullish. Though (as mentioned before) it does not completely eliminate run time errors, allows to reduce a bigger part of them.

It can be tedious, it can be annoying, but also it can help you a lot.

Collapse
 
f1lt3r profile image
F1LT3R

Finally someone with a Typescript preference! :) good points. It would be surprising if Object Composition itself was responsible for getting people lost, are you certain it wasn't an organizational issue?

Collapse
 
dotintegral profile image
Artur Siery • Edited

Well, when you have 50+ contentful models with references to other models then yeah... From the data modeling point, the model makes sense. So I would argue that no, it was not an organisational issue. And honestly, if I have big project that I need to work on, I don't really want to spend precious brain power to memorize all of the models. I let TS hint me with fields ;-)

Thread Thread
 
f1lt3r profile image
F1LT3R • Edited

Make sense. TS hinting is pretty great. Hinting tends to drive me nuts, but it is really useful in a larger/unfamiliar codebases. VS Code has some level of hinting for regular JavaScript. I find that I am still able to traverse the model to a satisfying degree (see image).

VS Code Hinting, Traversal, Peeking in Vanilla JS Projects

Thread Thread
 
dotintegral profile image
Artur Siery • Edited

JS Hinting is great, but it can only go as far as to the code/objects that you explicitly define. There is no way (unless I missed something) for VS Code to predict the structure of data incoming from API. With TS, you can just state that you expect data of this structure and hinting will work correctly. Granted, there is still possibility that you made mistake in typing the response, or the response changes after some time, but the same problems would occur in normal JS.