Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - using TypeScript vs Flow vs?

I'm currently learning React and I think I understand it pretty well. However, there's one thing that's been bothering me regarding development of robust React applications - what tools do developers use for static type checking?

I really like TypeScript. I think it reduces the pain of developing JavaScript applications quite a lot, thanks to type checking and other neat features. Visual Studio Code also offers a really nice code completion. And I know that I can make it work with React by using typings + DenifitelyTyped.

The thing is, there aren't many tutorials about using React + TypeScript. There also doesn't seem to be many articles about developing using this combo. On the other hand, many people seem to be using Flow, which is a project backed by Facebook (and I guess they also use it).

I've managed to find a discussion on Reddit with pros and cons about going the React + TypeScript / React + Flow way. However, to me, it appears to be quite outdated as it is about 10 months old now. I think a lot has changed since then.

I've also found two articles about using React + Flow and React + TypeScript. The author states some issues he's run into when using both of the options and concludes that TypeScript is "the best bet right now" (November 2015), especially because the Flow project has many issues and receives low developer activity from Facebook. He also mentions it doesn't play well with Babel?

So, I guess the question would be: Is it safe to use the React + TypeScript combo, or will I run into some difficulties? What about Flow? Are there some other similar tools I should check out? Which approach would you recommend?

Update September 2017:

Having more than a year of experience with daily use of TypeScript, and playing with Flow for a while, I've came to the following conclusions:

  • TypeScript is still painful to use to this very day. The problem is that the JavaScript world just moves so fast that TypeScript keeps lagging behind. Thinking about using that fancy new ES7 stage 3 feature? Nope, you can't. Wishing to get type hints for the latest version of some library? Wait a month, or two, maybe more...
  • Flow has come a long way, it has been improved a lot, it can catch some things that TS can't. Best of all, it finally works on Windows. Also, there's great plugin for VS Code (no idea why it has only 3/5 rating). And it works 100 % with React Native, TypeScript is not even 50 % there yet.
  • Most of the time, you don't need types at all. All the additional typing is rarely worth it. JS is a dynamically typed language, get over it :)

TL;DR: If you plan to use any type checker, I recommend using Flow.

Update February 2019:

I believe the recommendation above got out of date and is no longer relevant. Three reasons:

  • React Hooks are here. They make type-checking your code much easier. In most situations, no additional code is needed.
  • TypeScript's type inference has improved.
  • TypeScript has much bigger community than Flow. Even yarn, Facebook's package manager, is moving away from Flow, to TypeScript.

So, I think TypeScript is a much more pragmatic choice than Flow in 2019.

As to whether it's even worth using any type checker at all, I'd say it depends on the project size. Small projects probably don't need it.

like image 471
Honza Kalfus Avatar asked Apr 26 '16 17:04

Honza Kalfus


People also ask

Should I use Flow or TypeScript?

TypeScript has the largest community when compared with Flow. This translates to more documentation and more community support. We can clearly see this by comparing both tools on Stackoverflow, for the [Typescript] tag we can see 150,000+ posts, while for the [Flow] tag we can only see around 900 posts.

Is Flow faster than TypeScript?

Faster than Flow 🏎: TypeScript is much faster than Flow and doesn't seem to consume as much battery life as Flow when working on your laptop.

Is React better with TypeScript?

React Code and File type in TypeScript vs JavaScript From one glance, it looks like the code from a typical React TypeScript and JavaScript has no difference at all. This is not at all surprising as TypeScript is the father of JavaScript — It is the Superset of JavaScript.

What is the difference between Flow and TypeScript?

This is where TypeScript and Flow differ: TypeScript implements both a type checker and a transpiler that emits plain JavaScript. Flow only does type checking and relies on Babel or flow-remove-types or some other tool to remove type annotations.


2 Answers

I'm going to start this answer saying that I have never used Flow, so I can't say much about it. But, we are using React and TypeScript at work and it works great.

We have all the benefits I imagine you already know about, like refactoring, type safety, autocompletion, etc.

Sure, for what I have seen, the Flow syntax is cleaner than TypeScript, but you can add your types using TypeScript incrementally. I think, this is more a matter of taste. Some people prefer to have the code explicitly typed, others prefer to type less and have a stronger type inference.

About, the technologies I'd say TypeScript is a safe bet, Microsoft is pushing the language (there will be a version 2 soon), Angular is using it as well and there are a lot of Angular developers. Even here on SO, the tag TypeScript has more than 4K followers and it's rare to have an unanswered question.

The big issue with TypeScript, at least for us is that from time to time, we decide to use a component or a library that does not have the type definitions, so we have to create them ourselves. But I guess, that's a way to contribute back to the community.

like image 113
thitemple Avatar answered Oct 02 '22 14:10

thitemple


I just asked myself the same question (though not with React) and found the following articles useful in evaluating the two:

  • http://michalzalecki.com/typescript-vs-flow/ (2016-07-15)
  • https://blog.wearewizards.io/flow-and-typescript-part-2-typescript (2015-11-20)
  • https://blog.wearewizards.io/flow-and-typescript-part-1-flow (2015-11-13)

The approach taken by the flow designers feels more functional with better type inference and a better approach for nulls. However, TypeScript has better community support especially with respect to pulling in types for third party libraries via http://definitelytyped.org/ which is important for having types flow through all your code for maximum type safety. TypeScript is created by Microsoft which has a rich history in writing compilers and evolving the technology in favorable directions - notable here is C# and the fact that they are already adding non-null types (2016-07-11): https://blogs.msdn.microsoft.com/typescript/2016/07/11/announcing-typescript-2-0-beta/

TypeScript seems like the safer bet today.

And for those trying TypeScript out in an existing codebase I found the following settings in my tsconfig.json file really helpful in allowing TypeScript to co-exist nicely with JavaScript (allowing transition one file at a time):

{     "compilerOptions": {         "allowJs": true,         "isolatedModules": true,         ...     } } 
like image 42
Spritely Avatar answered Oct 02 '22 13:10

Spritely