Hacker News new | ask | show | jobs
by irrational 1709 days ago
Does Typescript get better? I've been forced to use it for my most recent project, but haven't been given any time to read through the documentation. I've found that I'm spending about 99.9999% of my development time trying to figure out how to get my IDE to not show that their are typescript problem. At this point I hate typescript with the burning fury of a trillion suns. I wonder if this is everyone else's experience?

Edit: So I take it from the downvotes that everyone else is a genius and I'm the only one that has had a problem learning TS.

7 comments

My experience is that the worst part is the config file. Its documentation isn't great and it's got a lot of cruft and overlapping functionality that can be confusing. It's easy to end up following a guide that's outdated or simply wrong in some subtle way that just happened to work for the original author but was still incorrect, when it comes to the config file.

Once it's set up, though, it's wonderful. No more refreshing only to discover you typo'd something or left out a step. You can hand your code to someone else—or to your future self—and they can often just start using it without having to ask questions, read documentation, or read the code to figure out what it does, because the types convey a ton of info and their editor/IDE presents it to them as-needed.

It lets you get your ideas about how the code works out "onto the page", as it were, and in a format in which a machine can automate most of the looking-up and finding-relevant-information-for-this-context parts. It slows down some code-writing a little (mostly by making you document things that should probably be documented anyway, either with tests [yes, tests are documentation] or in a manual or whatever) but speeds up using code so much that it more than makes up for the cost.

It's an incredible communication tool.

If you rarely need to communicate with others or with your future self about code you're writing, then it may not be worth using. So, if you're on a smallish solo project that you don't intend to stop working on until you're done working on it forever, never plan to hand to anyone else, and that you spend so much time working on that the whole thing's in your head nearly all the time, it might be fine to just write JS.

That was my experience too and yes, it gets better. I went from "I don't understand it" to "I hate it with a passion and want a proper typed language" to "uh okay it's caught 2 bugs I overlooked. This is useful" during my first project.

My advice: accept it's completely alien and is going to take effort to learn. I found spending a weekend sitting down and reading a theoretical guide was useful; there's good recommendations further up this page. Understand there's different levels of applying TS: one of its maintainers told me to start off slow and use `any` wherever I didn't know what to put. I pooh-poohed that because everything should be typed in a typed language but he was right. I have a few `any`s and a few `x as Type` where the code can't work it out because something's gone wrong. It works and my next project will be better.

I still have no idea (and haven't found any tutorials) on how to type form data (DTOs) or any object where stuff is structured but can be optional or required. How do you validate the type definition? What about HTTP responses which might have data in multiple structures - how do you type each of these depending on what's received? All answers welcome.

I will get there. And so will you. Onwards!

Aside: I got roundly mocked on a JavaScript framework's discord asking questions about edge cases to help me build a mental model of the language. Apparently it's "b.shit" and "questionable" and "weird way to learn the language". Well... now I know them I can infer what's going on, understand the workarounds used and why things don't translate from other languages. If that's the way you learn, embrace it. I still think JS (and TS by extension) are weird in places. Neither would be my choice of language, but I'm coming to appreciate them.

Once your environment is configured and you're writing good types, the only errors you see are when you do something that violated your type rules - like calling myRecord.names gives you an error "Property 'names' does not exist on type 'PlayerRecord'. Did you mean 'name?'"

I don't know if the issues you're having is bad setup, or type system abuse. Try a typescript forum or chat group?

I'll admit that, coming from C++, I'm 100% bought in on the usefulness of type systems; but I can't imagine refactoring or making major changes in a project written in untyped JS.

Yes, of course. But that’s the case for all new languages. You need to learn it. After some time you will be much faster in programming with a typed language as in a untyped.
Typescript is very easy, super powerful and lovely. If you are having problems is because there is something you are still not getting, remember is just Javascript but with a super set of tools that make it beautiful
So you didn’t read the (very easy to read) documentation and then complain that you can’t figure out how to use it? And then you wonder why people are downvoting you?
I haven't been given time to read the documentation. I was told TS is simple enough that I can just figure it out on the fly.
Well you have been told wrong. However it only takes about an hour to read the type docs. So take the time why not? Isn’t that better than being frustrated?
Try fixing the problems
That is exactly what is taking 99.99+% of the development time. Fixing the problems with typescript. My JS code has no issues.
Your original question of "does typescript get better?" can be rephrased as "does any tool become easier to wield with greater/better understanding?" and the answer is "yes".

Without a [concrete] example of what type of issues you faced, I doubt anyone can give you actionable advice, though.

> My JS code has no issues.

Either the types were wrong, or the code was wrong, either way there is an issue. Since you said you're inexperienced with typescript, it's possible the types were erong.

There are certain JavaScript patterns where TypeScript can be a bit inexpressive/unergonomic (especially with a lot of "metaprogramming" or "runtime polymorphism"[1] involved).

[1]: Most of the cases in our company the difficulty of expressing some types of "runtime polymorphism" was what I call thoughtless polymorphism (ie, it usually hides runtime errors that you're unaware of, especially if there's a proliferation of `anys`s in the code)