Hacker News new | ask | show | jobs
by s4vi0r 2847 days ago
As somebody migrating a React codebase from JS to TS, I can't believe how popular TS is.

It seems like something that only exists due to the popularity of Angular, where I can only assume the experience is significantly better than with React.

The type system is frankly disappointing, and the errors the compiler spits out at you (besides the most obvious ones) are almost always useless or gibberish and at times even more harmful than helpful.

It's better than nothing I guess, but I can't help but feel extremely underwhelmed with it given how many people use it. Are all of the people in this thread Angular developers or something?

5 comments

I can believe how popular TS is and it is super useful. I would not write a React application of any noticeable size without TS (and just FYI I would never willingly write an Angular app). The reason that I get far superior code completion and meaningful warnings / errors is sufficient to add a few annotations here and there.

Many people are doing TS wrong. They add types everywhere. Honestly, you should write as many types as necessary, but not more. Use type inference which works remarkably well.

At least add them on function return types and annotate types on common objects so that you don't have objects lingering with wrong property name/type or redundant properties that would be confusing to review.

It helps to create a type reference for your database schema, so you don't try to insert something weird or insert with missing columns.

> The type system is frankly disappointing, and the errors the compiler spits out at you (besides the most obvious ones) are almost always useless or gibberish and at times even more harmful than helpful.

I'd love to see examples of such errors. (And, ideally, accompanying code example)

I'm not at a desktop or laptop where I could provide you with a proper example, but the most obvious pain point is HOCs.

I was receiving an error complaining about a missing prop on a component I was wrapping with `injectIntl` from `react-intl` and could not for the life of me figure out what was wrong. Searching the type error wasn't helpful, and it wasn't until I really dug down and spent about a half hour of my time that I realized it was because we had something like `injectIntl(connect(foo, bar)(SomeComponent))` instead of `connect(foo, bar)(injectIntl(SomeComponent))`

The type error did not in any way make that clear or obvious. If I had to compare it to anything, it would probably be the type of error messages you see if you play around with type level programming/dependent types in Haskell. Or the types of error messages you see when writing Clojure.

I feel you and I think it is more of a "React + TypeScript" problem than a TypeScript problem.

We have started using Angular 2 (now.. 6?) with TypeScript for a project last year and I never had big issues with TypeScript.

Last month, one of our teams started a React project and given the success of using TypeScript before, they opted to do the same with their project. I've walked them through a few things but found myself getting frustrated a lot with weird TypeScript errors. Especially things like typing your Props was such a nightmare that we resorted to "any" a lot more than I'd like.

The project is not using TypeScript 3 so I am unsure whether that would get rid of some problems but React + TypeScript was just a frustrating experience for us.

Using TS3 with ours and the problems are definitely still there.

I think the most immediate and obvious pain point for me is typing HOCs. It's basically a matter of rearranging how you apply them until TS can automatically infer a type for you. At the same time, using `compose` instead of applying them individually solves that problem but results in strange types (that are different from the type TS infers if you apply the HOCs manually yourself in an order that makes it happy) that I suspect will cause type errors down the road when we convert the files that make use of it to TS as well.

What TS version are you on? The error messages are definitely one of the weak points, but they've been massively improved for many cases in 3.0.

FWIW I use Typescript with React, have had very few bad experiences(but not zero) and I'm never going back.

The most recent, 3.0.1 I believe? Or 3.1.0 - whatever it is.

As far as going back goes - it's preferable to plain JS I guess? But that's not saying all that much. I don't know - I'm sure I'll appreciate it more after this migration period is finished and things start to stabilize, but as of right now I'm definitely not enjoying it.

I used TypeScript with Mithril and I really liked it. I'm not going back to plain JS ever again.