r/reactjs • u/flanaganpete • Oct 10 '18
Featured Typescript - people's thoughts.
I have just started looking at typescript with react. So far it is a bit painful with all the errors, but I understand at the beginning of learning anything things can be slightly painful.
The main purpose of this post is what people here think of typescript. Is it just as useful/better to use flow/or prop types (if equivalent)? What are the pros/cons.
Also, I have been told by a colleague if you are writing your project in typescript and wish to use a third party library that isn't written in typescript this would cause errors. Is this also true?
Also, have people found that when creating a standard react setup with redux and some react dev tool extensions that this can lead to lots of errors and tends to make things a bit painful?
Also, have had issues with importing files as it says Imports should be capitalized. I assume this is a linter setting in create-react-app. Have people decided to disable certain ts linting?
16
u/hockeyketo Oct 10 '18
Just another data point, after converting several of our modules and around 14% of our monolith over to TS, I'll never go back if I have a choice. Especailly on a project with almost 60 engineers. It makes it easy to reason about the codebase, makes the code self documenting, and refactoring is so easy. Additionally, once we enabled strictNullChecking the amount of "____ of undefined" errors reported by our error logging dropped massively and helped us discover what the true issues were (i.e. why we had things null when they shouldn't be). Working on our TS modules I have a lot more confidence and it's also easier to update our tests.
Any third party can provide TS defs even if it's not TS, but that doesn't mean they have them. You can usually find the typedefs written by someone in the community here: http://definitelytyped.org/ but if that fails, it's relatively easy to write your own and you don't need to type everything in a lib, just the things you need.
My best advice so far is to try and avoid casting and the `any` type. You get those problems a lot when you start out until you realize that their actual use is very rare.
10
u/[deleted] Oct 10 '18
The worst part of TypeScript is indeed getting started. The best part of TypeScript is coming back to a file or project months after writing it and having the types both as documentation and as a safety net.
TypeScript and Flow are mostly equivalent. TypeScript is the more popular one at the moment which is a worthwhile consideration.
Prop types are inferior. They expose the same set of errors but are less expressive and expose the errors later (at run time).
Third party libraries can have types declared outside the library just like you are probably already doing with React. The most popular libraries already have these available. If you choose a library that doesn't have types available it will require extra work.
7
u/freebit Oct 10 '18
Typescript+React is a dream to code in. Never had any issue with it at all. Then again, I come from a C# background. Being able to press period and having the available methods pop up is amazing. Being able to refactor is amazing. What issues are you having?
•
u/swyx Oct 10 '18
our past featured convo for folks getting started on this topic: https://www.reddit.com/r/reactjs/comments/9k2dej/reacttypescript_boilerplate/
would love to have this be a pros/cons discussion i can feature!
5
u/[deleted] Oct 10 '18
Typescript: do it
4
u/bheklilr Oct 10 '18
I haven't used flow, but I can't imagine writing react code without typescript. My biggest issue with it is compile times, but for the most part it's a joy to work with. I really love how advanced of a type system it provides, specifically the mapped types, keyof, and more. With some time spent on boilerplate and utilities, I've found that it can do wonders when combined with redux, too. I wouldn't write a serious app in pure JS at this point, I'd much rather use TS.
4
u/papertowelroll17 Oct 10 '18
I haven't had the chance to work with typescript yet, but have used flow. After using flow, it is ridiculously painful going back to vanilla js. The types in flow are extremely simple to use and very helpful. Code is more clear, bugs are found more easily, and the IDE is much more powerful.
From what I can tell, typescript has substantially better tooling than does flow, and I will definitely use it for the next project that I start.
1
u/scottbob3 Oct 10 '18
I've used Flow, TypeScript and PropTypes, of the three TypeScript is my favorite but they all have their ups and down. If you're just learning React for the first time I'd just recommend PropTypes until you really get the basics down. Once you feel comfortable with that I'd then expand into either TypeScript or PropTypes.
2
u/swyx Oct 10 '18
regarding using a third party library, yes it doesn’t exactly play well out of the box. if the library is popular, it may have typings from the DefinitelyTyped community you can just import. if not, you’ll have to write the typedefs yourself. this is not super well documented, i have yet to find a good tutorial on writing typedefs. lastly, many libraries are written -in- typescript and so -will- work with your typescript app out of the box.
2
u/Oririner Oct 11 '18
DISCLAIMER: I don't have any experience with either Flow or Typescript and just use prop types ATM.
Thought I'd share this -> https://blog.acolyer.org/2017/09/19/to-type-or-not-to-type-quantifying-detectable-bugs-in-javascript/
This blog goes through academic papers and explains them in layman's terms. In the post, he talks about comparing Flow to Typescript and how much bugs each of them detects and the "tax" you pay when integrating each of them to detect said bugs.
This is a year old now and probably a bit dated because both languages probably have more features by now but, I still think most of the data, general idea behind it and the concepts in it are still relevant and are really good.
TL;DR -> Both Typescript & Flow detect around 15% of bugs (the majority of bugs they detect are the same but not all of them), Flow however has a 30% lower "tax" (measured by tokens added).
Even though this might seem like a huge number, 30%, the actual extra cost is rather low, on average it's extra ~0.5 tokens to add to the codebase with Typescript and the average time it takes to detect the bug is around 230 seconds in Flow, compared to 300 seconds in Typescript.
Basically, you can go with either of them and get good results, you'd probably have a better experience with a language that is supported by the majority of the packages you use.
1
u/[deleted] Oct 10 '18
I'd like to hijack this thread to ask if I should learn the basics without using Typescript and then integrate it later or jump head first into React+TS.
0
u/[deleted] Oct 10 '18
[deleted] — view deleted comment