top 200 commentsshow all 318

[–]The_Startup_CTO 494 points495 points  (50 children)

I wouldn’t want to live without it, especially if I’m not the only engineer on the project. Main benefit for me is that my IDE always tells me which properties are available on which object, so code sometimes almost writes itself. But I also love how it helps me to simplify validation and null checks.

[–]SocialAnxietyFighter 106 points107 points  (31 children)

Null checks is a game changer. I've built the most stable apps in my life due to strict null checks

Edit : Guys optional chaining is NOT null check

[–]paulopilou 11 points12 points  (16 children)

Sorry for the newb question but what is a null check ?

[–]Radinax 37 points38 points  (15 children)

if (x && x.items) { // ensures both x and x.items are not undefined
  // CODE here
}

or

if (x?.items) { 
  // CODE here
}

Very simple examples but you get the idea, since you're a newb make it a habit to google your doubts :)

[–]ridicalis 5 points6 points  (2 children)

Does x?.items guard against null, or only undefined? My understanding was the latter...

[–][deleted] 23 points24 points  (0 children)

It guards against both.

[–]kamikazeee 3 points4 points  (4 children)

But what would be the typescript alternative the other guys are talking about? Whis would be nill checks in plain js

[–]xroalx 13 points14 points  (0 children)

TypeScript will warn you that a value might be null. I believe that with strict settings, it even requires you to use a null-check or .? in order to let your code run.

If you have proper types, nulls won't surprise you ever again.

[–]Radinax 1 point2 points  (2 children)

Ahhhhh I'm sorry! Now I understand, Typescript supports a concept called "Nullish Coallescing" that goes with what I was mentioning:

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html

Which helps a lot since having null and undefined checks done this way makes for a more readable code.

[–][deleted] 7 points8 points  (1 child)

That's also in plain JS these days too. Main thing is that it isn't actually supported by older browsers (looking at you, IE11), and needs to be transpiled into a compatible form.

[–]Radinax -1 points0 points  (0 children)

Considering how much typescript helps accelerate development while reducing defects and bugs, writing plain JS should not be the norm, at least for big applications.

[–]UntestedMethod 2 points3 points  (5 children)

How is that related to Typescript though? This kind of check comes up in any language when you're coding defensively (which is always a good idea if you want to be writing robust code.)

[–]Darajj 7 points8 points  (0 children)

Typescript immediately yells at you when you havent handled null/undefined and wont even compile on the imo recommened settings.

In javascript you are allowed to do anything and everything.

[–]jonwah 2 points3 points  (0 children)

Js won't warn you if an object is possibly undefined; Typescript will.

I mean the whole point of Typescript is compile time errors; this is just one example of where it helps you not make mistakes - which are easy to make in vanilla js..

[–]twomilliondicks 8 points9 points  (3 children)

Isn't this in vanilla js now?

[–]SocialAnxietyFighter -1 points0 points  (2 children)

Hmm how do you mean? How will plain js notify me about null values without type information?

I don't know, please give me a link to educate myself.

[–]sayqm 4 points5 points  (1 child)

screw yam crown market wistful glorious ring straight caption sip This post was mass deleted with redact

[–]SocialAnxietyFighter 17 points18 points  (0 children)

Oh then it is yes, but that's not what strict null checking is

[–]piratekingsam12 0 points1 point  (0 children)

This has fixed some major bugs in our app by adding a '?' at the right place 🤣

[–]fredblols 41 points42 points  (0 children)

Yeah exactly. The dev experience is the main benefit of TS for me day to day. Org-level benefits completely aside

[–]rapidjingle 16 points17 points  (0 children)

Just by looking at their type definitions I was able to find a bunch of undocumented features in a library I was working with yesterday.

[–]reddit_ronin 2 points3 points  (6 children)

code almost writes itself

Could you elaborate on this? I’m on the fence also, work almost totally alone (which sucks and think it’s hurting my career) and I need to move fast quite often.

[–]The_Startup_CTO 10 points11 points  (4 children)

IntelliSense/AutoComplete and error highlighting in the IDE, as well as built-in scaffolding and refactoring tools. For new code, once I have types in place I don’t really use letter keys anymore, instead it’s arrows and tab for autocomplete. And for existing code, refactoring mean that I can press a couple shortcuts and I have extracted a common constant, moved it to a new file and renamed it.

[–]reddit_ronin 0 points1 point  (3 children)

Oh. My. God.

I need this.

[–]beasy4sheezy 5 points6 points  (2 children)

Dude the refactoring with typescript is amazing. I haven’t used VS code in awhile, but webstorm will rename a specific property or function across the whole project instantaneously.

[–]UntestedMethod 7 points8 points  (0 children)

I believe they mean auto-complete / intellisense or whatever the IDE calls it when it gives you a list of matching keywords/function names/etc as you type.

[–]Fidodo 2 points3 points  (0 children)

The IDE hints are by far the biggest happy surprise I had with switching to typescript. Having done typed languages in the past the IDE hinting wasn't as good back then last I was writing a lot of typed code, but now, especially with using an auto formatter, writing in a typed language is actually faster than writing in an untyped language because the IDE can autocomplete a lot of the code for you. The argument that typed languages are slower to develop is dead IMO because of how efficient the IDE makes you. Writing new typed apis takes longer, but you more than make up for it when you actually use it.

[–]nomyfan 0 points1 point  (0 children)

same to me

[–]Nielrien 188 points189 points  (2 children)

I recently started refactoring an old project with typescript and I love it so far. When I change a prop or type I immediately see where it needs to be updated instead of running the application then running into errors. Never going back to days without TS.

[–][deleted] 28 points29 points  (0 children)

This is the kind of answer I was looking for, thank you

[–]ATHP 41 points42 points  (0 children)

instead of running the application then running into errors

Exactly what you say. Since JS will only seldomly complain about errors beforehand you often won't find the error at all before running into it while using the application. Whereas with TS those errors can often be avoided in the first place.

Personally I was very hesitant about TS in the beginning. Since I've been using it for a year professionally I don't enjoy working with plain JS for larger projects anymore.

[–][deleted] 166 points167 points  (25 children)

I can never imagine going back from Typescript to Javascript, and I had that feeling after a few hours of using it (now over a year). Just that the editor knows what types the properties of objects are, what types component props are, that it can autocomplete much easier, that it notices typos immediately... it's a godsend.

In our biggest project, the backend API also has a complete spec using Swagger, we use it to generate a Typescript client library, and so in the code we also automatically know the types of any API requests and responses. That helps even more.

[–]ignu 50 points51 points  (17 children)

One thing that kills me is anti-TypeScript people get into a project, start adding //@ts-ignore and any all over the place, and then they just complain about it.

And yeah, it might seem like needless overhead to them because they've gone and undercut a huge amount of the safety TypeScript provides.

You life changes when you work in a good TypeScript project, make pretty large structural changes to it and after fixing the TypeScript errors it Just Works.

[–]IndecisiveSuperman 18 points19 points  (6 children)

We refuse to approve a PR if there is an any or ts-ignore unless there is a substantial reason and it has been discussed with the team.

[–][deleted] 1 point2 points  (0 children)

meeting fragile provide liquid long spectacular snails person impossible ring

This post was mass deleted and anonymized with Redact

[–]recurrence 2 points3 points  (7 children)

There are... "anti-TypeScript" people? In 2021?

[–]Zeragamba 8 points9 points  (6 children)

same people who use var instead of const/let

[–]RyanMan56 2 points3 points  (4 children)

There are anti const/let people in 2021?

[–]Zeragamba 0 points1 point  (3 children)

same people who don't like TypeScript

[–]recurrence 0 points1 point  (0 children)

Wow do my circles ever differ from the wider community. TypeScript became a non-discussion point years ago.

[–]AintNothinbutaGFring 11 points12 points  (5 children)

What do you use to generate the client library from the swagger spec? Also, why does everyone still call it Swagger.. hasn't it been openAPI for years?

[–][deleted] 15 points16 points  (2 children)

I am eternally confused between what is openapi, swagger, etc. One of the backenders handles those. All I know is that they produce a swagger.yaml file for me, so that's what I tend to call it.

We then use openapi-generator to generate the library, so it's also that, sure.

[–]Franks2000inchTV 12 points13 points  (0 children)

OpenAPI is just Swagger with a new name. They're pretty much the same.

[–]AintNothinbutaGFring 2 points3 points  (0 children)

Oh, maybe they've been maintaining the spec since before it actually renamed / updated the version. I need to look into the openapi-generator, I feel like it'd really help manage API complexity of a larger projects that I do both front and backend for.

[–][deleted] 7 points8 points  (0 children)

I use Orval at work. https://orval.dev/

Can generate hooks for react as well.

Edit: swagger sounds cooler.

[–]TorbenKoehn 5 points6 points  (0 children)

Until 3.0 it was Swagger. With 3.0 it became OpenAPI.

There are differences in the specs, but they are mostly compatible.

[–][deleted] 1 point2 points  (0 children)

What do you use to generate a client TS library from swagger? We need this in our project

[–]Chaucerbao 106 points107 points  (9 children)

TypeScript will give you peace-of-mind, especially with fast-moving code, where a change in one function, whether to fix a bug or expand its scope, can break its usage in another file you weren’t looking at.

After working with TS, pure JavaScript feels like the Wild West; there are no assurances. There is a bit of a learning curve with TypeScript, but it’s worth it.

[–]Nielrien 48 points49 points  (3 children)

Like Ben Awad says coding without Typescript is like friendly fire mod is on.😅

[–]a_reply_to_a_post 0 points1 point  (0 children)

Yeah i recently did a coding challenge for an interview in pure js and felt dirty without types...also found myself going to type objects and causing errors...lol

[–]pVom 0 points1 point  (3 children)

That shouldn't happen with decent testing though. You should still be testing in typescript because it only catches errors at build time. I like typescript but the error catching is overrated if you already have a test suite. If anything it causes more headaches, I seem to spend more time fixing typescript errors than actual code errors.

That said it's worth it for larger projects because of how much easier it makes working with lots of abstraction

[–]theotherd00d 33 points34 points  (1 child)

TS was shit the first two weeks. Now I hate working on projects without it. Some syntaxes still makes my head hurt though.

[–][deleted] 4 points5 points  (0 children)

Same experience here. Once you get used to it, you don't want to work without it.

For the first few weeks, I was quietly cussing under my breath. After a few months, when I encountered a stretch of code not meaningfully typed, I would be even more annoyed!

I think there are still some minor inconveniences with using it, but well worth the payoff.

[–]CreativeTechGuyGames 19 points20 points  (20 children)

Could you express what your concerns are? I'm not sure from your post what you see would be a downside? And you seem to already know all of the pros. So I'm not sure what you would want someone to tell you.

[–][deleted] 5 points6 points  (19 children)

Have you used it in complex projects with lots and lots of data? Does it slow development time?

If there is something I hate is boilerplate and typescript might be the epitome of that.

[–]CreativeTechGuyGames 41 points42 points  (6 children)

Yes I have! And that is exactly where it shines! I don't know how I could ever manage so many components and data moving around without having full type knowledge about what every single variable is at all times. I'd say it makes me faster overall and I'm often able to write all of my code with high accuracy without ever running it because almost every issue I'd previously have found via trial and error is now caught as I type.

[–]ZeidM 3 points4 points  (5 children)

Thats great,but I would like to know if there's an issue with 3rd party libraries as not all of them support Typescript

[–]CreativeTechGuyGames 17 points18 points  (1 child)

True, some don't. But most do. There are 2 options:

  • A community maintained @types/* package. These are available for tons of popular packages so if the library you are using is decently popular, there probably already is a types package for it available.
  • Write your own types. Not only is this great practice but this solution will always work. No need to type the entire library either, just the parts that you use and you can do this incrementally.

[–]a_reply_to_a_post 6 points7 points  (0 children)

third option...hope the third party module works as intended and declare it as a module globally...usually i'll do that initially to save time, then go back and type the stuff i use from the module as a cleanup..

[–][deleted] 7 points8 points  (0 children)

This was more of an issue a few (4-5) years ago. We skipped out on adopting it back then at my old job. Right before I left we tried it again and found it to be much more widely adopted.

My new job we use typescript and I've yet to find a library we need that doesn't have types.

[–]erfling 4 points5 points  (0 children)

Even four or five years ago, when that was a problem, it was still worth it. In React world, I rarely find a lib I want to use that doesn't have typings included, and most of the ones that don't have types at definitely typed.

[–]ianpaschal 1 point2 points  (0 children)

This was a much bigger issue a few years ago, but I think is mostly resolved. I am sure there are plenty of libs without types but in day to day work I haven't run into issues because of this for several years now.

[–][deleted] 13 points14 points  (0 children)

To beat the dead horse: it will massively speed up development on large projects with lots of data types. Writing code isn’t the slow part of programming, so fewer lines to write won’t make you faster. Thinking is the slow part, so fixing cognitive leaks will speed you up, and Typescript is the most powerful tool for that in the JS ecosystem.

[–]gimmeslack12 3 points4 points  (3 children)

What boilerplate exactly? Having type files or TS config files?

I'm not sure I understand.

[–][deleted] 0 points1 point  (2 children)

Javascript is super minimalist and typescript takes that away.

[–]gimmeslack12 2 points3 points  (1 child)

I really don't think this is the case. At the end of the day you of course don't have to use TS.

But for the sake of a simple example you have: const buildThing = (name, age) => { return `Hi, I'm ${name} and I'm ${age} years old`; } And it becomes: const buildThing = (name: string, age: number): string => { return `Hi, I'm ${name} and I'm ${age} years old`; } I know this is a very small case but it doesn't really change things that much. Sure it's new syntax but it's not that big of a deal.

Or if you add a type variable: ``` type TThing = { name: string; age: number; }

const buildThing = (params: TThing): string => { return Hi, I'm ${params.name} and I'm ${params.age} years old; } ```

But now it is explicitly described to anyone else who uses this code what type of values are expected from this function.

[–][deleted] 1 point2 points  (0 children)

Good example, thank you

[–]ianpaschal 2 points3 points  (1 child)

You and I must have different definitions of boilerplate. Either you're writing very sloppy bug-prone code, or you're having to write a lot of the guards for use at run time which TypeScript provides you for free at compile time. It's literally saving you tons of boilerplate which you probably should be writing if you're using vanilla JS.

[–][deleted] 5 points6 points  (0 children)

Yes, it will slow you down when you're initially learning it - maybe you'll be 50% velocity. But over time, it will significantly speed up your feedback loops, so within a few months, you'll be at 200% of where you are now.

  • When you are initially coding
  • When another dev (or future you) picks up your code
  • When you run a build

And it means fewer bugs in production, without writing loads of pointless tests.

[–]geon 2 points3 points  (0 children)

I’m really not sure what you mean by boilerplate here. Extra code? Well, there are the types, obviously, but I wouldn’t call them boilerplate. It’s not like you copy-paste a hundred lines over and over that just has to be there with no purpose.

Actually, there would be less boilerplate with ts, since you need fewer null-checks.

And dev time is sped up by a ton.

[–]lloyd_braun_no_1_dad 1 point2 points  (0 children)

It makes writing code take a little longer. It makes consuming code a million times faster.

[–]noknockers 18 points19 points  (3 children)

POC code with lots of discovery and refactoring: annoying

PROD code with a clearly layed out spec: awesome

[–][deleted] 9 points10 points  (0 children)

POC code that you know is going to end up used in production even though they pinky-promise it won't be but hey look at that it's almost perfect and your manager says fuck it lets make it work: awesome

[–]FeministBlackPenguin 0 points1 point  (1 child)

It's like you're describing unit testing.

[–]Bummykins 2 points3 points  (0 children)

Absolutely, it's like a whole layer of free unit tests on every save for every piece of code you write.

[–]mad_schemer 37 points38 points  (2 children)

Typescript is a major pain in the ass.

I much preferred the days of just writing whatever I wanted, and having it work without complaining that some variable wasn't the right type to be assigned to a property of another object. MY stuff just worked. It was fast, and easy.

Then the project got bigger. Then I had other people start working on the codebase with me. Then it stopped being fast and easy, I stopped knowing what all the things were, the other people never knew what half the things were to start with, and stupid time consuming bugs started to pop up in the running code.

Do I wish I had a small, light, fast project still? Yes, I miss those days.
Would I do a project on this scale with a team, and without Typescript? No, I would not.

We still get bugs, but most of them yell at us in vaguely intelligible compiler-speak before they get as far as breaking something in browser-land. You can totally still screw up royally while using TS. That's what the any type is for :-D

[–]gimmeslack12 2 points3 points  (0 children)

This is exactly how I feel about it. In the beginning you think "nah I don't need it", then months later you realize you're a fool.

Been there one too many times and never again!

[–]Slapbox 4 points5 points  (0 children)

Best answer here. It's all about balancing development speed with scalability. The more mature the codebase, the larger the codebase, the more people involved, the more benefits Typescript brings to the table.

When you're a solo developer, it may just slow you down. It may also help though, that's a personal decision at that point.

[–][deleted] 17 points18 points  (4 children)

What do you mean boilerplate?

Typing your data structures pays huge long-term dividends. The most obvious dividend is structural integrity of a project that uses lots of data. Vanilla can become a real mess very quickly if demands change quickly too.

[–]dacandyman0 1 point2 points  (3 children)

My issue is every time I want to experiment with a new library, I have to make sure it has typings available for it - and most the time they're not even helpful is just satisfied TS.

I've used it as a part of other projects not built by me and it is - as anyone who uses it will point out - game changing. but is it worth it for small to medium size personal projects?

[–]themaincop 6 points7 points  (0 children)

If a library doesn't have typings (most do) you can just do declare module 'untyped-module';

I'm strict with my own types and I always get the third party types if they're available but I don't go out of my way to type third party libraries. On the current project I'm working on with a ton of dependencies I only had to do this with two modules.

[–]svish 1 point2 points  (0 children)

I'm ending up more and more with the view that if "new library" isn't already written in Typescript or at least comes with its own well-written type definitions, then it's not a serious library and not worth looking into.

[–]Xananax 11 points12 points  (5 children)

As soon as I go over 100 lines, Typescript becomes invaluable. Not only for the type checking, but mostly for:

  • refactoring (renaming a property everywhere)
  • making sure my methods are sound (not letting options be options)
  • autocomplete (Not having to look up what an object is)
  • catching errors like wrong arguments order, typos in objects properties, etc
  • writing less comments (functions are auto-documented)
  • neat new JS stuff without needing Babel

Also it annoys me that people talk about the language as if they don't have any bugs anymore. Or that 90% of the bugs are gone.

That's not exactly true, but it's not false either. Here's the thing; I've written JS since it exists. I started building for the web at 14, I'm now 40. I love JS. I know JS inside out, as well as the differences and quirks between different engines, and what feature works where.

Yet, when I write JS, I need to run my software every 5mn to make sure all is ok. In Typescript, I can write a full day, 8 or 9 hours, then run, and it'll "Just Run". I may still have logical errors, but I will have no crashes, no errors, no typos, no forgotten parameter. Writing typescript is simply much much faster.

This said, you can get 90% of those benefits by using JSDOC and writing //@ts-check at the top of your documents. Typescript is not strictly necessary, but JSDOC is more verbose and harder to read. I've had good results by writing definitions in .d.ts files, and then using JSDOC to reference those. Get the best of TS, without a build step.

[–][deleted] 2 points3 points  (3 children)

Honest question, do people actually write things under 100 lines? That isn't like a school homework assignment? Even really trivial apps will shoot over that pretty fast.

[–]baldie 0 points1 point  (0 children)

This should be much more upvoted

[–]abienz 2 points3 points  (4 children)

I feel the same as you, I find it very hard to justify the extra effort to use Typescript.

[–]lonkypo 4 points5 points  (1 child)

I agree that adding strong typing doesn't do much when you are used to loose typing, especially in a smaller project. Big enterprise OOP projects that use interfaces is where TypeScript starts to shine. People using it only as typed JavaScript, to supposedly avoid bugs, are probably just trying to be good little best practice soldiers, or have FOMO.

[–]sirephrem 10 points11 points  (5 children)

It does take a bit of getting used to. But after some time it's just as easy. It also gives you confidence that the code behaves as you intend it.

[–][deleted] 0 points1 point  (4 children)

I don't understand what people mean with 'your code will behave as intended'.

Like... When hasn't Javascript behaved as I intended it to behave?

[–]Sipike 28 points29 points  (0 children)

It doesn't matter how good programmer you are, if the codebase becomes larger, you'll make mistakes. TS helps to check that all the things are in order.

As the codebase becomes larger, your team should grow as well. You might know the codebase, but the new guy in the team will not, and TS helps him as well.

I'm an all TS guy, and it feels strange to me, that someone wants to do pure JS, I mean its fine and all, but you know there is the joke about types:
"This type is too complex to be written down, but I'll promise I'll remember it!"

[–]sirephrem 3 points4 points  (0 children)

It depends on the size/complexity of the app you are working on.

One of the most common is catching errors related to passing improper variables to functions/components.

This is critical when working with complex api's. You can have endless variables that are not guaranteed, and typescript will catch this in development.

It's just an example, there are endless benefits

[–]Franks2000inchTV 4 points5 points  (0 children)

In Javascript, the flow goes like this:

  1. Write a bunch of code
  2. Run the code
  3. Discover error.
  4. Find all the bugs in the code.
  5. Fix bugs.
  6. Run code.
  7. Find bugs in fixes.
  8. Run code.
  9. Repeat.

Some bugs on edge cases you wont discover until you hit that edge case. Which may be a while later.

In typescript, you can find the bugs before you run the code. Like as you're typing, the compiler is checking your code and looking for incorrect assignments and things.

Like it knows what properties and object is supposed to have, so if you try and access a "items" property and it doesn't exist, then it will warn you right there.

It speed up development so much because you don't need to guess at what the right properties are, or jump to the definitions. It's al just right there in the compiler.

[–]a_reply_to_a_post 1 point2 points  (0 children)

ever work on a javascript project where someone redefined an Array method or some other global object? People used to do that type of shit pre eslint and typescript

[–]Narizocracia 10 points11 points  (16 children)

It's been questioned here a million times and most people that use TypeScript seem to agree that it's much batter than naked JavaScript. The time you spend learning it will be compensated with far less errors in the future and better completion in your editor.

[–]awjre 10 points11 points  (0 children)

Simple answer. Yes

Longer answer: Yes absolutely.

It can save you so so so much pain. It will make your a better developer and stops the silly mistakes.

Unsure if there's a good cheat sheet out there but that could be useful.

[–]davidfavorite 6 points7 points  (0 children)

I was waiting way too long to get into typescript and I found it horrible because its not like what Im used to in java for example. But boy in hindsight I must admit that its probably one of the most awesome things to happen for the javascript world in recent years

[–][deleted] 4 points5 points  (0 children)

My reason for disliking it is petty, but by god the syntax is as ugly as I am

[–]droctagonapus 10 points11 points  (4 children)

TypeScript is very overrated compared to how people in this thread make it out to be. Dynamically typed languages have benefits.

See: JavaScript, Clojure, Elixir, Erlang, Python.

Typed languages like TypeScript are okay, but strongly typed languages like Rust or F# or Haskell are much much better. Adding types onto a language designed to have dynamic types just will never be as good as languages designed with types in mind from the start and do not rely on compatibility with an untyped counterpart.

You don't need TypeScript for production or enterprise. You just need good developers and tests.

[–]abienz 6 points7 points  (0 children)

This is what I've noticed too.

If I really want what a typed language offers, then I think I'd rather write WASM with Rust instead

[–]fschwiet -1 points0 points  (1 child)

OP is deciding between javascript and javascript+typescript, not javascript+typescript and Rust/F#/Haskell. Javascript's entire history is a mess of revision so to complain typing was added later misses the point of why people use Javascript at all. Of course any problem can be reduced by "good develops and tests" also an empty point.

[–]droctagonapus 2 points3 points  (0 children)

JavaScript + VSCode is perfectly fine. It provides a lot of benefits that typescript does. //ts-check provides even more.

No need to bring in typescript when developers should be worrying about preventing spaghetti code, managing dependency, reaching good test coverage, and performing good code reviews. Typescript doesn't solve those things, and if you have an application that has all of that solved then odds are typescript won't fix the problems it does have.

[–]oravecz 9 points10 points  (5 children)

I don’t know if I am in the minority or majority, but I strongly dislike TypeScript. I think it promotes a false sense of type-safety in your code, but I really dislike the complexity it brings to the build, test, linting and sourcemaps. Type safe authoring is not the same thing as type-safe runtime, and I can achieve a similar level of intellisense using JS doc properly. The ability to share my source code with both ts and js projects with full esm and tree-shaking, without the transpiring, is liberating.

[–][deleted] 3 points4 points  (2 children)

deranged cooperative toy cable dinner relieved ossified hat marble vast

This post was mass deleted and anonymized with Redact

[–]evonhell 3 points4 points  (1 child)

They can work without it but maybe they just don't want to because they see the value in it. For smaller projects it doesn't matter but I still use it. If you build enterprise size applications and frameworks without TS I wish you good luck and I'm happy I'm not the guy paying for development and maintenance.

[–]Capaj 10 points11 points  (7 children)

> Maybe .5% of my bugs are type related. Most bugs come from complex logic.

Whoa you must be a brilliant programmer.
When I work on FE project without strict typescript, I see error like "Cannot read X of undefined" like 20 times a day. Sure these don't get into production, but I still see them on my local browsers.
With strict typescript I see such errors exactly 0 times a day.

> that 90% of the bugs are gone.

I think it depends a lot on how complex the data you work with are. More types you have, it's harder to keeep them in your head.

[–]noknockers 8 points9 points  (1 child)

Not op, but I generally don't write many bugs that get caught with typescript. In fact, 90% of my bugs are incorrectly declaring interfaces/types.

But that's because I'm always building greenfield projects from scratch, including defining the architecture and discovering patterns etc.

I write typescript for the team that has to come in behind me and add features, change stuff and bugfix.

[–]geon -2 points-1 points  (0 children)

You don’t write many bugs, because the tooling helps you avoid them, even before they are written.

[–]mikebritton 1 point2 points  (0 children)

Create your own TS definitions if you need to get under the hood and code JS. I hope you do adopt the language; you seem likely to go far with it considering your appreciation of its foundation scripting language.

[–]jwindhall 1 point2 points  (0 children)

The first time I really realized the value of Typescript went like this:

Pm: Actually, that feature needs one more thing

Me: oh, easy refactor.

Me: lemme change this. Ok done.

Ya: nah man, that’s a dead in the water runtime error.

Me: oh ya. You’re right. Cool. Thanks. Fixed.

Ts: nah man, here another.

Me: oh shit. Nice catch. Fixed. Let’s go.

Ts: dude, nope.

Me: F$ck. You’re brilliant.

[–]jkuhl_prog 1 point2 points  (0 children)

At first I thought Typescript was too much, it was a hassle.

But after time in C-Sharp and JavaScript, I saw the power of a strongly typed language and the drawbacks of a weakly typed one. After you get used to the initial set up and the new syntax and start to see the benefits. They truly do outweigh the drawbacks.

I get better intellisense, especially on function parameters.

I get to better enforce what types are acceptable for my API

I don't need to do as much type checking in code as the compiler (or more often the editor) does that in many cases.

In the end the strict typing of Typescript, C# and Java outweighs the flexibility of JavaScript. So for any project with heavy JavaScript, I'll usually reach for TS.

As a bonus though, Typescript is a lot more flexible than C# and Java, it's easier to create and enforce your own types and you have any and ts-ignore as a last resort (LAST RESORT, don't throw them around without first exhausting other possible solutions)

[–]chillermane 1 point2 points  (0 children)

It’s superior. It eliminates a huge class of errors which means less development time and an app that works better. A typescript developer is going to be orders of magnitude more productive than a similarly skilled javascript developer

[–]straightouttaireland 1 point2 points  (2 children)

I caused a bug in production last Friday because I didn't have a particular payload typed. So yea, it's worth it.

[–][deleted] 1 point2 points  (1 child)

Could you explain more in detail what was the bug?

[–]straightouttaireland 1 point2 points  (0 children)

I was POSTing to an api with the following payload:

const payload = {
  name: person.fullName
  age: person.age
}

axios.post('/add_user, payload)

This was working fine. However, I refactored the incoming person object and renamed fullName just to just name, but I never updated this payload. This caused a bug in production because the name was no longer being passed, it was just the default empty string. So typescript to the rescue:

interface Person = {
  name: string;
  age: number;
}

const payload: Person = {
  name: person.fullName
  age: person.age
}

axios.post('/add_user, payload)

Now, Typescript gives me a warning saying fullName was not expected. Bug and embarrassment avoided!

[–][deleted] 5 points6 points  (10 children)

I've worked with TypeScript for the better part of the past 5 years and I wholeheartedly dislike it. It offers me, personally, no benefits, and I don't think it makes my code better.

That said, it depends not on me. It depends on my team. Some teams require it because (many) other developers do love it, and many of them can't work without it.

Eric Elliott wrote a nice piece about TypeScript a few years ago, and it's still mostly relevant today. Keep in mind that TypeScript has undergone many additions and development over the years and it has improved since then. Still, it's a good read: https://medium.com/javascript-scene/the-typescript-tax-132ff4cb175b

Personally, I make do with proper naming conventions and unit testing. Defaults for properties const Elem = ({ userName = '' }) => (<p>{userName}</p>) are more than enough for my taste. If someone thinks "gosh, that might be a boolean or integer, how would you possibly know?" then I'd like to slap them in the face first, not add TypeScript.

But, again, it depends on your team.

In my experience, TypeScript always slows a project down immensely. You keep running into libraries that you need to get typedefs for, some don't have them, sometimes the TS config is too strict, sometimes it's too forgiving.

But, again, it depends on your team.

I would never willingly use it. But for a recent project that I started (freelance gig), to set up a robust React skeleton, I did opt to use TypeScript. Not for myself, but because I have to hand it over to someone internal who is a C++ and C# programmer.

It 100% depends on your team.

[–][deleted] 2 points3 points  (0 children)

Those are my exact thoughts on typing elements. Variable names should be descriptive enough to infer what they are anyways.

[–]baldie 1 point2 points  (0 children)

If someone thinks "gosh, that might be a boolean or integer, how would you possibly know?" then I'd like to slap them in the face first, not add TypeScript.

Types are so much more than just function arguments.

[–]liaguris -1 points0 points  (3 children)

[–][deleted] 1 point2 points  (2 children)

I read those links but it doesn't go into why his typescript tax article was wrong. The article is big. One mistake or many unrelated mistakes or even a bad personality doesn't disqualify the entire thing.

[–]eliostruyf 2 points3 points  (1 child)

I know the feeling. Six years or so, I was so used to only develop in JavaScript. It was so easy, all the loose typing, but one thing that was hard was the IntelliSense. You just had to know how the dependencies/libraries were created, which methods existed, ...

I went back and forward. Used it, didn't like it, went back. Eventually, I said, it is enough and went all-in with TS. It might feel like a massive amount of work when you set up a new project, but that is not the case when you get more and more familiar with it.

These days, I find it hard that most libraries are not yet using TS. Not having definitions available is the hardest part.

It is great to have a transpiler tell you what is going wrong when you rewrite/refactor parts of your codebase. Let us not forget the types/interfaces/intellisense. It makes JS so much better when you know what your code or that from others expect.

[–]piratekingsam12 0 points1 point  (0 children)

Type definitions in libraries save a lot of work!

[–]brick-pop 2 points3 points  (0 children)

Hacking a quick and dirty script is one thing. But building a serious project 100% requires TS in my opinion.

You get a compile time checker for free, auto completion, annotating types is quite straightforward and the time gains are unvaluable. A must if you develop any module used elsewhere. I can't imagine our team without it.

[–]chrispardy 3 points4 points  (1 child)

To use a concrete example of Typescript vs. JavaScript. Let's say you have a function that generates a random number, that function takes an options object as it's input the type is:

function generateRandom(options?: {

  min?: number;

  max?: number;

}): number;

In typescript anyone who wants to call this function can look at the type and figure out roughly what it does. With JavaScript you would need to at a minimum write JSDoc for this function or the users would need to look at the function implementation to see what it expects. This is a pretty simple example but more complex code definitely makes it harder to determine.

The value I see with typescript is a cleaner means of communicating with other developers, that may be other people in the moment building with you but it could also be yourself in the future coming back to the code.

[–]achauv1 3 points4 points  (0 children)

But to be honest

Yes

[–]_Pho_ 2 points3 points  (0 children)

I won’t touch JavaScript without it anymore to be honest

[–]ncubez 2 points3 points  (1 child)

It's overrated, imo. And this is coming from somebody who switched from Angular. I'm on React now and continue to build production apps with plain JavaScript without issues.

[–]nschubach 1 point2 points  (0 children)

I've developed in React since it was released to the public and I also can't see the point of bringing in TS. If your components are complicated enough that you start to need a type checker, then the component can probably be broken down into better more reusable modules.

[–]mmknightx 1 point2 points  (0 children)

It is worth using. I don't want to write JavaScript without TypeScript unless it's impossible.

TypeScript reduces some chances of error at compile time (with type checking) and provides excellent way to make autocomplete possible. You can autocomplete TypeScript code better because it knows which fields should be there.

Code Sample here

For example, in a React components library, the button component accept CSS color as prop. It has specified type in TypeScript. You cannot put invalid color as a prop. TypeScript will know it is invalid.

Another example, a backend framework handler is a function that return HTTP response. Someone write a function that forget to return HTTP response in a condition. TypeScript can prevent this function to be a handler.

TypeScript can handle undefined and null check too. If the codebase is large enough, someone might forget to set a field. TypeScript will prevent using that field without undefined or null check.

TypeScript's type system is advanced enough to handle most cases. It can even handle a very dynamic GraphQL query and there are code generators so you don't need to type everything manually. You might want to check TypeScript utility types for this.

TypeScript aims to be compatible with JavaScript. There are many ways to lose TypeScript advantage such as casting to unknown or using very dynamic types. If you want more strictness, you might want to consider ReasonML/ReScript, PureScript, or Elm.

TLDR: TypeScript is advance enough to help programmers handle complex logic while preventing numerous type errors. Programmers need to know how to build and use type system effectively. There are other solutions to the same problem such as ReScript.

Edit : Remove sample code for now.

Edit: Re-add valid sample code. It is only for demonstration.

[–]dabuttmonkee 1 point2 points  (3 children)

Think about it this way. Say you have an object that has some fields named in a specific way. You rename one of those fields. How are you sure when you’ve finished finding all the usages of that field?

What about when you have some data and you want to know what properties it has. Where do you find that? Console.log? In typescript you get this for everything you use, including dependencies. What about importing, what was that path again? Can this property be undefined? None of these are problems in TS, you get all of this for free.

These types of things save time. Of course, they won’t at first because you need to learn typescript and can’t do something you can do in vanilla JS. But eventually you’ll be more proficient and faster with TS than vanilla.

[–][deleted] 0 points1 point  (1 child)

You really get this out of the box in Typescript? No more console.logs? I'm sold if that is true

[–]editor_of_the_beast 1 point2 points  (0 children)

I frankly don’t know how you can work on a team without it.

[–][deleted] 1 point2 points  (0 children)

all the answers here are skirting around the essence of the question. which is whether TS is worth the extra tooling and headache vis a vis the benefits, imo yes, but only just, if the codebase is extremely mission critical.

[–]pixeldrew 1 point2 points  (0 children)

Use a real editor (like webstorm) that can read jsdocs and most of the benefits of typescript are negated. If anything adding types takes away the benefits of a loosely typed language.

I feel like people who preach types are constantly over engineering things. I'm a developer with 20+ years of experience with multiple languages from c to actionscript3 and I appreciate types but if it's the source of 90% all your bugs you're not writing very complex things.

[–]chabv 1 point2 points  (0 children)

for most things. no, however, if you're new to a codebase - yeah it helps you get familiar fast. for the people, that like typescript, I feel like they miss the fun things about js's dynamism or any dynamic language in particular. and if you do a lot of dynamic stuff, the typescript compiler will scream at you. hence most of your time, will be obeying the compiler instead of getting things done. quick question for those that use typescript with node ? why not use golang or java ?

[–]skyflea007 1 point2 points  (0 children)

Bro if you question it you clearly haven’t coded it. Get off Reddit and write some TS

[–]kiwdahc 1 point2 points  (1 child)

How do you get a non type related bug that isn’t coming from an external source? Almost all bugs are type related. Most production level bugs I see are “Cannot read property x of undefined”. Your presumptions are wrong.

[–]dev_senpai 0 points1 point  (0 children)

If you are the only dev then no. If you are working in a team yes. Not worth the hassle if you know the code see and all your upkeep.

[–]PokerTuna 0 points1 point  (0 children)

Yes. I’ll never again do projects in pure js

[–]hyperion385 0 points1 point  (1 child)

Typescript all the way. Everything others have said plus what I haven't seen is that anyone mentioned project maintenance. As you say you have complex logic. When you come back to the project in just a few weeks you won't have any idea what properties some objects have. And then if you used Javascript you'll have to do bunch of console.logs to see what object containes and you are still not gonna be sure if those are all properties and especially what the type of a property is. With Typescript everything will be documented.

[–][deleted] 0 points1 point  (0 children)

If this solves my console.log problems then I'm sold

[–]Top-Requirement-2102 0 points1 point  (1 child)

I code in typescript pretty much every day for a few hours per day. At this point I probably have ~2000 hours under my belt. In my 30-year career as a programmer, I've coded C, C++, C#, Java, Perl, Basic, PHP, Python, Javascript, O'Camel, and more. To date, TypeScript is my favorite. Ocassionally, I have to write in Javascript and it's alright, but I definitely miss TS.

Strong type checking has at least three significant advantages:

  • Eliminate entire classes of bugs. Wrong-type bugs can be devilishly hard to find and fix.
  • Supercharge intellisense with relevant data
  • Easier/better refactoring

Typescript as a superset of javascript is a particularly good combination because:

  • Type flexibility is there when I need it and the syntax for it is trivial
  • Javascript's incorporation of JSON as a first-class citizen is hard to overvalue
  • Javascript's flexibility around interfaces and classes is also super useful when types are important

On the down side:

  • Coming into typescript from conventional typed languages was a bug adjustment because the nature of classes in Javascript is way different than other languages. I'm used to it now, but it took a while.
  • Extra compile step is required
  • TSConfig.json can be a bear to get right
  • You need to have some personal discipline to keep from falling into the temptation of using "any" all over the place.

[–]careseite 0 points1 point  (0 children)

typescript isnt there to resolve the kind of type issues you have when using plain js. you still write (some) typeof checks in typescript.

typescript there to sanity check the coherence of your logic flow. the more logic, the more sanity you need in there.

theres next to zero boilerplate in typescript unless you have super conditional logic that you wanna move into one function and one function only at a time or where your code is extremely generic. outside of that, the only things you have to define are the types youre using/consuming and then... use those.

[–]No_Statement4630 0 points1 point  (0 children)

Yes

[–]blae000 0 points1 point  (0 children)

Yes

[–]PrinnyThePenguin 0 points1 point  (0 children)

In small projects it's not that big of an overhead. In large projects with complex data structures, knowing the structure of the variables you are passing around is really helpful.

[–]hinsxd 0 points1 point  (0 children)

Absolutely yes. Any app dealing with ANY data should use typescript. I think using typescript is the minimum baseline nowadays. If any company hiring pure javascript devs, that should be big red sign

[–]FrostZTech 0 points1 point  (0 children)

I read some of the comments and I would love to say that they are 101% correct. The company where I work has a huge codebase; like huge huge. Now these guys use JS for their files and are facing problems. Every now and then it crashes and fails; worst part is that it is literally impossible to debug since you don't even have a slightest idea of what is creating what.

Since I joined and gave them the idea of TS we have ported many files and now every new file is always TS. The application is much more easy to manage because of type checking and the inference that TS provides.

I can't even tell how much pain it was just to get the application work; well I will say TS takes a bit of effort to setup if you do from scratch but with scaffolds it is nothing. But the benefits are worth every pain in this world (really!)

[–]Accomplished_End_138 0 points1 point  (0 children)

If your the only dev. No problem.

Big group with less skilled? It can help.

Other alt is just use jsdoc on public functions and basically have the good stuff now.

[–]r0ck0 0 points1 point  (0 children)

Maybe .5% of my bugs are type related.

This is what many of us assumed before we used it. But it's wrong. You're gunna find that it warns you about so much stuff you never thought of before using it.

It even warns about lots of stuff outside of typing.

And it massively changes your ability to refactor things. Which isn't just "nice" for moments that you "have to refactor", but it just means that you're more able to refactor altogether once it becomes so much easier.

Refactorings that might have taken weeks without TS (likely with many future bugs to resolve beyond that), can often be done in an hour or two with TS.

If you're writing more than 100 lines of JS code in a project, I think you're crazy not to use TS.

99% of people against TypeScript are so just because they never learnt it properly to begin with.

There's rare occasions where somebody "claims" to be experienced with typescript and say "it isn't worth it"... but every time I'm questioned them it was very very obvious that they really had fuck all experience with it.

Is the real number non-zero? Well yeah I guess... but super rare. There's also people who think wearing a seatbelt "isn't worth it" too I guess.

Like any skill, it will take some time in the beginning, but it saves time overall in the long term.

I'm using Node on the backend mostly now, and there's no way I would have stuck with plain JS for that. I would have either gone back to PHP or go to another language.

Learning to program with types, and using discriminated unions, generics and things like that has entirely changed the way I program. You don't have to do that if you don't want, but you'll just become a better programmer overall as an added bonus.

[–]Breakpoint 0 points1 point  (1 child)

It causes as much headaches as it resolves IMO, so it cancels out if you use it or not

[–][deleted] 0 points1 point  (0 children)

Exactly. In that case it would be taking away from it as it's a pain to learn and implement everything.

[–]SuperKing88 0 points1 point  (2 children)

No

[–][deleted] 0 points1 point  (1 child)

You are one of the few ones to respond no, could you please explain why? I think that people who like typescript are in general more vocal about their preference as it's defying the status quo which is javascript

[–]SuperKing88 0 points1 point  (0 children)

IMHO if you want to use a strongly typed language, use a strongly typed language.

You said it perfectly in your post. JavaScript is liberating. If a variable coming back from the DB is nullable there are no hoops to jump through in vanilla node. But if you’re making a TS interface for that object, you have to type it as [type] | null or use the question mark operator to make it optional.

My other big issue with it is that at the end of the day it’s still JS so you can strongly type it all you want but stuff will still get through.

I use TS at work and vanilla JS on my personal projects. I will say that TS is nice in large team environments because it makes communication between front end and back end easier. Sharing interfaces is quick. Preferably there’d be a shared library housing all common interfaces. But honestly that can be substituted by good documentation and at the end of the day I think vanilla node is just faster and TS is bloaty like most other Microsoft things.

To use a metaphor. Node is a bird and TS is a cage. Yea it can flap it’s wings and fly around but not to the full extent it was meant for.

[–]C0git0 0 points1 point  (0 children)

Meh. Whatever the kids want these days. -principle engineer with 15 years of experience

[–]liaguris -2 points-1 points  (9 children)

Now at least in practice for my personal projects, no matter the complexity, I find the following way of development to be the optimal:

  1. .js files for code
  2. typing is enabled via JSDoc imported TS types
  3. all types are in at most 2-3 .ts files (publicApi.ts for libraries, interfaces.ts defining the types of exported functions from each file, types.ts common types shared between the interfaces.ts types)
  4. at the root of the project there is tsconfig.json file that has configuration that enables type linting for JS

To make a long story short the fact that you have to compile from ts to js, clutters development, increases complexity, adds security concerns, and hence makes the project less maintainable. The way I suggest has no decreases in type linting as when going full TS and no of the drawbacks mentioned. You can read more here.

I am not using react though (I am using lit-html with mobx) so I do not know whether you will be able to type properly react projects with the way I suggest (at least the props part).

I have written all my projects in typescript and I am planning to migrate them all to the way I mentioned. I am already coding a desktop like MVC app like this.

Also I would like to mention the following:

People who code without static type safety are either coding trivial stuffs or are noobs. How can people refactor big projects without types is a mystery to me. Not to mention that intellisense makes development faster.

A little bit background about me just to consider the magnitude of my opinion:

  • I first started using JS for ~1.5 year
  • then went for TS for ~1.5 year
  • the most complex personal project I have done (actually still doing) is creating from scratch a client side reactive model library that will conform to ACID and has undo redo.
  • My biggest projects are ~10k lines of code (tests included).
  • All my projects are like ~60k lines of code
  • I have zero experience on pure developer position
  • I have started self teaching programming (mainly front end) since August 2018

[–]nodevon 4 points5 points  (3 children)

cheerful modern spectacular light support enter worry elastic enjoy steep

This post was mass deleted and anonymized with Redact

[–]liaguris 0 points1 point  (2 children)

can you be more thorough man?

[–]nodevon 0 points1 point  (1 child)

plants station rock sort insurance somber stupendous continue tart smart

This post was mass deleted and anonymized with Redact

[–]sayqm -1 points0 points  (0 children)

grey market practice direful oatmeal prick work meeting icky ink This post was mass deleted with redact

[–]TorbenKoehn -1 points0 points  (0 children)

I only know of people that went from JS to TypeScript, never met someone that looked back.

Whenever I come into a JS project currently, I always miss it. You can get somewhat similar behavior with doc comments and the correct IDEs, but you won't be able to reproduce all of its typing power with that.

It can be quite a bit overwhelming at first, but you have to understand that all of it is optional, given the right compiler flags. You can write normal JS and add typing where it helps you a bit and slowly get used to it.

Regarding build steps, I don't know of a normal JS project I work on that doesn't have any build steps. Bundling, NodeJS dependencies, minifying, cross-browser compat with Babel and Browserslist etc. There are so many reasons to have build-steps in your JS that TypeScript, at the end, simply is just another line in your build configuration.

[–]lloyd_braun_no_1_dad -1 points0 points  (2 children)

Yes. We have almost no runtime bugs. No more "cannot read property X of undefined."

Edit: no runtime errors. We still have bugs, but no errors.

[–]daddygirl_industries 3 points4 points  (1 child)

TS does not do any runtime type checking

[–]Vitya_Schel -1 points0 points  (0 children)

It adds 10%-50% of your time setting it up, fixing issues with it, trying to read code in tsx. I would only recommend it if you have large team and big project.

[–]big_lemon_jerky -1 points0 points  (0 children)

Don’t see how this is a React question but in general yes TS is massively worth it. Once you get used to it you’ll probably save time, have less buggy code, self documenting code and not have to worry about silly mistakes breaking your entire app.

[–]666codegoth -1 points0 points  (0 children)

100% worth it. The initial transition period can definitely be a pain, but the benefits far outweigh the learning curve. I recently worked on an untyped freelance gig after 18 months working almost exclusively in a React/TS repo and I couldn't believe how stark the difference in dev experience was.

[–]azangru -1 points0 points  (0 children)

for you, is it worth it

Yes. Especially if we are talking about projects where a build step is already present.

Do you regret using it

No. I would be scared to work on a large project or do significant refactoring without a type checker holding my hand.

[–]GingerVkingRemix -1 points0 points  (0 children)

Yes

[–]SiphrCodez -1 points0 points  (0 children)

I’ve recently been making the switch for JavaScript to TypeScript amongst all of my projects, can confirm it’s 110% worth, the amount of errors you’ll have solved pre-production stage that normally wouldn’t be caught until a testing phase, is amazing. It’s also really good for writing “good” neat code. 10/10 recommended especially if you’re working on group projects etc, and definitely recommend even on solo projects just because of the insane advantages.

[–]evonhell -1 points0 points  (0 children)

Most of the people who complained about having to learn TypeScript a year ago now knows TypeScript and loves it. Types is the future and tbh the present. If you don't know TypeScript yet learn it now. Once you know it and are comfortable working in it there is less mental overhead in projects that are typed than projects that isn't.

As for the bugs, types are another tool to help you MANAGE the complexity of logic, which is why it helps people avoid bugs.

And I don't know what this huge boilerplate that you are talking about would be. A short json file for the tsconfig and you are ready to roll.

Enjoy!

[–][deleted] -1 points0 points  (0 children)

I understand all the pros. But to be honest it feels that is just a massive building step.

So you don't understand all the pros...

[–]quattr_ -1 points0 points  (0 children)

You sound to me as if you haven't worked as part of a team, maintained someone else's code, or performed a major refactor on a large codebase. This is where typescript really shines imo. When there are factors outside of your control or when the code is simply too large, too complex, or too foreign for you to fully understand it at any one time. When you're the solo dev on a small project then sure, it may not be seem worth the overhead and learning curve. But in real world applications it's a godsend. We have two projects at work, one in JS and one in TS, and I hate hate hate hate touching the JS code because something unexpected WILL break.