Sitemap
Andrej Ocenas

Andrej Ocenas

Typescript and Flow

3 min readMay 21, 2016

--

I am a fan of static typing so when building www.whatlib.com I decided to try first Typescript and then Flow.

TLDR; I ended up using Flow as it has a bit better documentation and is less invasive when using it for existing project.

Typescript

But anyway, I started with Typescript as it seemed as a better solution at the start. And it actually is quite easy to convert. As advertised you can just change your .js and .jsx files to .ts and .tsx respectively, change your transpiler to tsc and thats it. In my case my project looks a bit like this:

\-root
\-dist
\-src

as I was already transpiling everything with babel and running my application from dist, because I want to use the same code/syntax on client and server.

The real fun begins when you do not want to look at all the compilation errors. My tsconfig.json looked like this:

{
“compilerOptions”: {
“module”: “commonjs”,
“noImplicitAny”: false,
“preserveConstEnums”: true,
“rootDir”: “./src”,
“sourceMap”: true,
“forceConsistentCasingInFileNames”: true,
“jsx”: “React”,
“moduleResolution”: “node”,
“target”: “es5”
},
“exclude”: [
“typings/browser.d.ts”,
“typings/browser”,
“node_modules”
]
}

With noImplicitAny = false you get less type errors but less type safety, but should be easier when you want to ease into it. Bigger issue is third party modules. For those you need to tell TypeScript compiler their definitions. You can do that manually (and you probably will have to anyway in some cases) or use Typings (previously called TSD) which is tool for managing external module definitions.

Get Andrej Ocenas’s stories in your inbox

Join Medium for free to get updates from this writer.

This is where I spent most of the time while converting my project. Typings have a notion of different sources of definitions, most used are probably from http://definitelytyped.org/ or directly from NPM packages, where package author can put typings info into package.json. Typings directly from NPM should be more accurate, but most of the time there are only definitions from definitely typed (“dt” source in typings output). To install a definition just do:

typings search module_name// for definitions from NPM
typings install module_name --save
// for definitions from dt
typings install module_name --ambient --save

Honestly I could not find good documentation for this but ambient means it loads definitions by default from “dt” source. Save argumen is similar to save in npm, and creates a typings.json where you have list of all installed definitions, presumably so you you do not have to check them into version control, similarly to npm and package.json.

But then I came to a point where I still had some errors, for example Object.assign was unknown. Reason was that my compile target was ES5 and could be fixed by creating my own definitions but I already lost my patience and went to try Flow.

Flow

The starting point for this was the same commit as with Typescript. The infrastructure setup is easier, you just need babel plugins, one for syntax and one for striping the type informations. The overall idea of Flow seems to be more geared towards incremental compliance, so I added

// @flow

comment to some files, it gave me some easily to fix issues. Also there were some files from node_modules which gave some errors so I just added them to Flow ignore list and that was basically it. Much less work than with Typescript. On the other hand, with Flows aggressive type inference, I am not sure if my code is more type safe than with Typescript approach. Probably the opposite, but for me, easier setup and gradual adoption was what I wanted.

One big positive thing is that for the most part it seems that Typescript and Flow type syntax is identical. I did not try it, but I assume that for the most part you could just switch to Typescript later on and your types from Flow would be picked up right away. You can also add typing info in separate files, defining classes, interfaces or module, which is similar approach as TypeScript definitions for external modules.

Some caveats:

  • I use WebStorm IDE lately which has some Flow support, so it won’t bark on it’s type syntax, but it does not give you any Flow type errors. I ended up using watcher that just outputs Flow info into console window inside WebStore. Not anywhere as nice as it’s Typescript integration but workable.
  • With Typescript you probably want to use TSLint, with Flow there are more plugins for ESLint. Also for ESLint you need to change its default parser to babel-eslint.

--

--