DEV Community 👩‍💻👨‍💻

Discussion on: Toxic flexibility - a case for TypeScript

Collapse
psiho profile image
Mirko Vukušić

Did my first 2 projects in TS. This article covers one of the better aspects of using TS vs JS. After 2 projects, I can say i still have like love-hate relationship with TS. Sometimes it feels like blessing, sometimes (complicated cases) gets me frustrated. Eficciency... I find myself doing less debugging, a lot less, code just works from the first try more often. But I find myself programming "arround" TS a lot more. Sometimes TS feels "stupid", and insists variable type is not known, while in fact it is (TS just doesnt get it) and Im forced to write more code. Sometimes it just saves me from a ton of little errors. Sometimes I spend hours to write a typesafe version of the function that would take me minutes to do in plane JS. Code is not as readabke as before in my opinion.

But bottom line, autocomplete just works and I couldn't live without it anymore. Also, I hope it's going to get better because in my opinion, TS code needs to be written different, apps architecture needs to be setup different, right from the start. JS app cannot just be converted to TS by adding types to existing code. And that's what I ws thinking before. So 3rd project will be better, I hope. :)

Collapse
latobibor profile image
András Tóth Author • Edited on

I agree with you fully; lot of it is coming from JS itself and the fact TS is a superset. It's like the great sand worm in Dune: you have to know your way around and then you can ride it. It is by no means perfect.

You are not saved from learning some TS hacks like let timerId: ReturnType<typeof setTimeout> = ..., when you have to deal with a mixed environment (node and browser).

Typescript must be also be written a certain way to avoid conflicts - I am planning to write about that as well (mainly how (not) to use any and as using instead generics).

However when the puzzles are solved the working type system is super powerful and gives me a lot of confidence when I have to do renamings, usage checks and other every day things, not to mention how superful powerful is when you have just npm install-ed a library written in TypeScript and you get all the properties, parameters and return types immediately. Forces you to have a simpler API which I believe in the long run is a good thing.