In defence of TypeScript
Hah, never thought I'd be the one to defend TypeScript, but here we are.
@dhh recently said he's done with TS. This caused a bit of commotion on X.
I remember the first time I tried it. I was appalled. What is this shit? Why would someone work with it? Is it techo-machismo? Setting up types and casting them and everything felt like a colossal PITA.
But it was the latest trendy thing at the time and I felt obligated to figure it out. I liked the idea of being employable. And eventually, similar to how my relationship with olives evolved since childhood, I developed a taste for it.
TS actually turned out to be kind of useful. It saved me time. Not upfront, but later. Like insurance. You don't see the benefit when you first get it and pay for it. The payoff becomes apparent later.
And looking at @dhh's and other X'ers reasons for not liking it - I dunno, I don't get it.
A lot of them say it takes long to set up. Does it? Really? Takes like 5 minutes. I recently set up TS and linting and Prettier for a project at work and the thing that took the longest was the damn ESLint. The TS part consisted of adding NPM libs and dropping in a standard tsconfig.json file. Done. 🤷♂️
Then some say it's "glorified static analysis". Well, yeah. That's exactly what it's meant to be. To automatically stop you from doing stupid shit that will create weird, hard-to-fix bugs in production. I mean, we use ESlint for a reason too, don't we? And tell me you have a spell checker in your code editor.
Another common complaint is that it get's too complicated to cast and fix type errors. Fair enough. But I found that the majority of the time a simple interface or type should do the job.
If you're running into situations where you need to do weird shit like unions and whatnot - that's on you and you might need to take a step back and reconsider your code and what you're trying to achieve with it. It often just means you need to refactor and simplify stuff a bit.
Which brings me to my next point. TS is meant to save you from bugs and help speed things up when working with objects and models used all over your app. That's it, IMO. And people seem to forget that. It's meant to save you from yourself. Your bad memory, sloppy thinking, your lazy reasoning.
Hell, I'd wager to suggest that for most cases, it even helps prevent the need for testing as a whole. Yeah, I said it.
To me, it's not meant to be a strict compile checker like you have with real typed languages like the real, smart engineers use. It's meant to help insure that that property you're referring to in the object you received from the API actually exits. And is spelled the way you think it is. And not spend another 5-10 minutes trying to unravel that whole irritating situation. Because TS will straight-up tell you you're doing something silly and the reason is why.
Hell, even to just know what properties exist on that random object you created 2 weeks ago. TS IntelliSense alone has saved me countless hours of browsing layered and nested files trying to figure out what properties are available on objects being passed around between different objects and services. It's like autocomplete for coding. If you sing praises to Copilot because it helps save you time, why not TS?
I don't know. I think both the haters and purists take TS too seriously. It's meant to help you keep track of complex code, models and catch bugs before they happen. The complexity of the whole thing is totally up to how anal you want to be yourself. TS is basically clever comments. Coding on rails.
You don't have to(IMO mostly shouldn't!) use all the fancy, complex features it has. But damn, it's nice to instantly see what I'm working with and to catch bugs before they happen.
TS is all right. It's a useful tool if you don't overcomplicate things.
But go ahead, pick your tribe.