r/learnjavascript • • 13d ago

Javascript is interesting but is also driving me nuts, how do you actually adapt to writing code without mistakes quickly?

I have been accustomed to C# and Python and have only recently been introduced to one of our projects where we use JS (express and react). I can write good code that eventually happens to work, but I keep falling for really unexpected pitfalls. I realized that most of my mistakes were calling undeclared variables, they are mostly errors that can almost never happen in python or C# because the compiler would immediately point to that piece of code and will usually prevent the app from running. That honestly took a good amount of time and a lot of energy. I understand that it's a regular part of learning, though I want to speed it up.

I know that the browser can ignore these issues so the app ends up running without realizing a problem. Also, that debugging part where console.log() is the best tool in our hand is a headache, it helps but I feel that being able to set breakpoints and move through the whole piece where a bug hides is more efficient.

Don't get me wrong, I am not hating on JS, I love it and I acknowledge that each programming language holds its advantages over other languages. I have in fact spent a good amount of time and put a lot of effort into understanding how to handle JS and react (not a lot but to an extent where I understand its rules and syntax, and can build with it). That's why I want to see how others who may have adapted to JS avoid making these rookie mistakes over and over.

Do not mention asking AI to fix issues or write code instead. I prefer to write code myself.

12 Upvotes

58 comments sorted by

View all comments

Show parent comment

1

u/delventhalz 12d ago

Honestly, if you are using TypeScript in loose mode, I question why you are bothering with it at all. TS is awesome when it can offer you rock-solid guarantees. When you leave gaps in those guarantees you are adding whole build step for what... better autocomplete? Doesn't seem worth it to me.

Anyway, yes the Svelte team and Turbo 8 team have both famously written about their transition away from TypeScript recently. If you want the details, I recommend you look up what they've written themselves. As you might expect, the reasons mostly boil down to complexity, both in terms of adding to the build process and adding to the mental load of developers. I probably wouldn't have made the same decision myself for Svelte or Turbo 8. For projects and teams of that size and complexity, I think the trade offs are worth it. But as the teams and projects gets smaller, I am more and more sympathetic to just keeping things simple.

Tools have to carry their weight to convince me to deal with them. TypeScript often does, but if I'm just throwing together a portfolio page or a small web app or something, no I'm not bothering.

1

u/Beginning-Seat5221 12d ago

Honestly, if you are using TypeScript in loose mode, I question why you are bothering with it at all.

You can add types to code that is tricky while keeping JS code everywhere else - it's the zero upfront cost, and totally opt-in approach. In practice mostly used by people transitioning, but you're arguing for using JS, so that's why it came up.

TS is awesome when it can offer you rock-solid guarantees. 

TS does not offer rock solid guarantees.

function write(arr: unknown[]) {
  arr.push(null)
}
const a: string[] = []
write(a)
for (const s of a) console.log(s.toUpperCase) // RUNTIME CRASH

Saying that makes you sound a bit lacking in knowledge. Personally I use a TS fork that prevents this error, stock TS is more of a type hinting system than a type guaranteeing system.

When you leave gaps in those guarantees you are adding whole build step for ...

Told you already, you don't need a build step... Node, Deno, Bun all run TS (erasable syntax only with Node). If you're deploying to browser then you've got a build process already, and you're likely using vite which has a fast TS compiler built in I believe. Even if you did have a build step it's typically about 3 seconds that can run on watch.

Anyway, yes the Svelte team and Turbo 8 team have both famously written about their transition away from TypeScript recently.

As I said already, Svelte still used TypeScript - they just wrote their types in the JSDoc format not the TypeScript format. It's the same type system and typechecker. If you want to use TypeScript via JSDoc you can... but that's not using raw JavaScript.

Funny, looked into Turbo 8 and here is Rich Harris' comment on Turbo 8 https://x.com/Rich_Harris/status/1699490194565578882 "removing types from your own code is clownish, epically misguided behaviour, but whatever — to each their own. removing types from a library THAT OTHER PEOPLE HAVE TO USE, however, is just user-hostile dickwaddery" - not sure Rich is the support you think here is here.

There will always be someone who bucks the trend. I don't see any compelling argue from DHH - he didn't like type gymnastics and using `any` - so his solution was to make all his code `any`. Yeah types are hard sometimes. But selective type assertions and fine and you learn to write code that is easy to type.

Tools have to carry their weight to convince me to deal with them. TypeScript often does, but if I'm just throwing together a portfolio page or a small web app or something, no I'm not bothering.

You can definitely write small projects without type checking if you want to, I wrote plenty in times gone by. Now I won't write a single script without it. It just feels dumb to me to run code and have it fail when the IDE would have caught it with TS enabled. For a simple script I experience virtually zero friction, I set up from a template with typescript and eslint, run dev, and start coding.

But it is true to say that TypeScript requires you to write code in a static-analysis way - before TypeScript I wrote quite different JavaScript, and there's some script that just doesn't play nicely. If you write type-unfriendly JavaScript, e.g. because you've developed strong habits working in JS, then I can see how TS can look like a bit of a monster, but that's totally alleviable by writing the kind of code you'd write in a statically typed language, which doesn't need difficult types. Personally these days I learn towards soundly typed systems (beyond what TS offers), I just can't be bother with the idea of writing code that has subtle bugs in it that I don't discover later, where a typechecker could have caught them.