Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of ES6 + Flow instead of TypeScript

I'm modeling a JavaScript app. The main feature of this app is to consume a REST API to configure and show forms with some custom input types. I'm thinking of use TypeScript to take advantage of types and classes. But after some Google searches I realized that I can reach a very similar result with JavaScript ES6 + Flow (and Babel maybe).

My questions are:

  • These two approaches are really similar or I'm making a mess?
  • What I should consider to make a good decision choosing between ES6 + Flow or TypeScript?

Thanks for help.

like image 468
Bruno Peres Avatar asked May 12 '17 15:05

Bruno Peres


1 Answers

Disclaimer: I work on the Flow team.

In general, I believe that Flow is more focused on soundness, while Typescript is more focused on ease of use. As such, Flow will disallow things that could cause runtime errors, while Typescript, in some cases, decides that in practice, certain behavior is not likely to cause an error, and preventing an unlikely error is not worth the inconvenience to the developer.

Here is a concrete example:

type Foo = {
    x: string;
}

type Bar = {
    x: 'foo';
}

const b: Bar = { x: 'foo' };

const f: Foo = b;

f.x = 'asdf';

This code is incorrect -- b.x is now 'asdf' even though the type says that it should be 'foo'!

Typescript allows this. Flow complains with a confusing error.

(typescript playground) (try flow)

With regards to tooling, Typescript wins, hands down (as much as it pains me to say it, as the person primarily responsible for integrating Flow into Nuclide). The setup process is simple and things just work. For Flow, you need to install and configure Babel (since Flow is not a compiler, you need something to strip out the types). For Typescript, all you need to do for editor support is download VSCode and you are done -- you don't even need to download Typescript separately. For Flow, if you want to go the recommended route, you need to install Atom+Nuclide and download Flow separately. Once you are set up, the Flow editor support works quite well, but the initial setup is time consuming.

I haven't used VSCode+Typescript enough to be able to accurately compare them but I do know that a lot of people are very happy with it, and based on my limited experience with it, it is very polished. The feature set is larger, too -- for example, Typescript supports automated refactoring.

Typescript's DefinitelyTyped (a set of type definitions for libraries, so you can use npm modules while retaining type safety) is larger and more mature than Flow's similar flow-typed -- although flow-typed is growing.

like image 141
Nat Mote Avatar answered Sep 30 '22 13:09

Nat Mote