this post was submitted on 14 Nov 2023
808 points (94.6% liked)

Programmer Humor

32060 readers
2092 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] -2 points 10 months ago (5 children)

As someone who is relatively new to webdev stuff, I gotta ask... what is the point of typescript? Like, is it faster than JS, does it have more functions or smth? To me it just looks like JS with extra steps and a really, REALLY cursed way to declare variables.

[–] [email protected] 18 points 10 months ago

As a beginner you don't see the benefits as it is indeed JS with extra steps. It's not worth it for small projects and prototypes, but once you start having larger projects where you need to refactor something, you'll see the benefits.

Also, auto-complete.

[–] [email protected] 12 points 10 months ago* (last edited 10 months ago) (2 children)

Types help you prevent errors while writing the code instead of while running. That's a massive benefit, as it literally makes a lot of errors impossible (as long as you don't work around it) - otherwise you have to write a lot of tests to get the same guarantees, and you could always miss something by doing that.

The other benefit is that it allows other developers to understand your code very, very quickly. Types describe what your data looks like - there is nothing more important in programming than that!

When you install an NPM library and your editor gives you hints about parameter types, return types etc., that's all Typescript types at work.

[–] [email protected] 4 points 10 months ago

After maintaining a huge JS codebase for years and finally upgrading it to TS, my life is so much easier. Refactoring is faster and less error-prone. I no longer have to manually document the parameter/return types for every function. I don't have that gnawing "oh damn, what if I missed something" feeling whenever I make changes.

Yes it's a bit more work up front but it pays dividends on larger codebases.

[–] [email protected] 3 points 10 months ago

Your answer and mine are complementary, they definitely complete each other! Well done!

[–] [email protected] 7 points 10 months ago

It helps for when you have a variable that's for numbers and you use it as a string or something else, it shouts an error. In other words, it protects you from yourself

[–] [email protected] 5 points 10 months ago (2 children)

No, it is slower than JS but it can be compiled to JS. The point of typescript is bringing static (or generally talking, predictable) types to variables, so that treating erroneously a number as a string should be more difficult. In a large codebase, it's easy to make mistakes and debugging is not instantaneous but it needs time. Typescript helps here. You write more code but it helps you out later

[–] [email protected] 2 points 10 months ago

I don't have much experience with TS, but in other strongly typed language it goes even further than string vs number.

For example you can have two numbers Distance and TimeInSeconds and even though they are both numbers, the type system can make sure that you won't do distance+time.

It can also let you do distance/time and return Speed type.

It will prevent many logical errors even though everything is technically a number.

[–] [email protected] -4 points 10 months ago (1 children)

TS syntax is still the most cursed thing I've ever seen

[–] [email protected] 9 points 10 months ago (1 children)

Then it just sounds like you've not written any typed languages yet

[–] [email protected] 2 points 10 months ago

I have some knowledge on C# and I've used C++ before... I really like C# (Ik it's not technically fully statically typed but it gets the point across), ASP.net just has too many hoops to jump through just to get a project started, it's kinda annoying.

[–] [email protected] 4 points 10 months ago

Typescript gives you better suggestions, red squiggles where you would get errors or bugs if you try to run it, more information about whatever it is you’re using that’s defined somewhere else, and some other neat stuff like project-wide renaming that works every time.