ShakaCode | ShakaCode Blog | Rails On Maui Blog | Rails | ReactJs | JavaScript | Webpack | Productivity |

TypesScript or Flow


#1

I’m suspecting we want TypeScript rather than Flow for new projects.

Here are some good links:

https://github.com/niieani/typescript-vs-flowtype
https://eng.lyft.com/typescript-at-lyft-64f0702346ea
https://redditblog.com/2017/06/30/why-we-chose-typescript/

Any opinions?


#2

I have no my own experience with Flow but I did some stuff with TypeScript.

In my opinion a typed language is - in the most cases - a blessing (may be because of all my experience with Pascal/C++/Delphi/Java/C#). There are some areas where Typescript convenience leaves to be desired (Immutable.js, Apollo GraphQL to name a few) but in most of the cases it really saves you a lot of time when you debug the code or just read it.

Which language is better - this question may not have an uniform answer (projects are different and people are different). I do agree with what people in the articles said about TS vs Flow in general. But I believe the discussion should be closed after this (from the first link):

typings for public libraries:

  • TypeScript: plenty of well maintained typings
  • Flow: a handful of mostly incomplete typings

TS is not an ideal language but in the language war it is not the best one which wins (those old enough should remember VB6 vs Delphi dispute and who won in the end). So I believe TS is the next big thing (for the next couple of years or so:))


#3

Before deciding on typing, a general question: how deep into typing will we go? Just static typing of variables, function returns, and interfaces? In order to make our deadlines, our team here will most likely be doing programming as well. This will be a bit of a learning curve for us. Are all your developers that could be working on this project already fully comfortable with these typing libraries?

Currently, we migrated a project to typescript. We used types for function returns, props, and state of react components.
It helps when you need to pass for example 10 props to the component. Some of them must be required. And typescript saves a lot of time for figuring out what kind of props must be passed.

Also, it saves the time when you use some typed function. You don’t need to go to the source to investigate what you need to pass and what is coming out. It’s all highlighted in the editor.

Also, it adds other small features to the editor like autocomplete and imports rewriting when renaming a file for example. I’m using vscode and it has the rich support of typescript. (both from MS)

Also, it has many type definitions for libraries including react, redux, lodash, moment and many others.

I have nothing against TS and have heard mostly great things. My only concern is the fact that its a big change from using pure JS and I’m afraid of having such a different ecosystem all at once that we’re not used to.

Since typescript is the superset of javascript, then you can use pure JS in typescript.
You can mix .js and .ts files in the project. Also, you can rename .js to .ts and don’t do anything until you need to add some types.
For typescript pure JS is the valid code.
Then you can add types gradually in places where they are needed.


#4

Probably the best read I have found on comparing the different typing engines is here: https://codeburst.io/inference-engines-5-examples-with-typescript-flow-and-reason-edef2f4cf2d3

At our work, we have chosen Flow and really love it. We originally chose Flow because we had a webpack/babel setup already so flow was easier to integrate AND because Facebook was using it. But having used it for a while now, I greatly prefer the Flow syntax versus typescript, the additional flexibility it provides, and broad ecosystem support.

The only downside to flow is that developers CAN bypass it - you can’t with typescript because of the compiler. The converse to this is that you can use newer JS features in the babel ecosystem long before Typescript adopts them. For example we started using optional-chaining some time ago (almost a year?) with webpack/babel, but TypeScript is still waiting for support (https://github.com/Microsoft/TypeScript/labels/Waiting%20for%20TC39) .

At this point, the Flow and TypeScript type definitions are both mature, Reason is lacking community support. Personally, I prefer the babel ecosystem versus waiting on M$.