Flow - A Static Type Checker For JavaScript
Written by Ian Elliot   
Thursday, 20 November 2014

If your biggest objection to JavaScript is that it is that it is dynamically typed, then Flow from Facebook might be just what you are looking for. 




Flow is an open source static type checker for JavaScript. Of course with a dynamic language there is no static type for any variable. What Flow does is to use type inference. In other words, if you assign a string to a variable then its type is string.

The problem with type inference is that it can be very difficult to work out the type of a variable at any given point in the program without actually running it and even then it is possible for the type to change with each run of the program. 

Flow does its best, however, to infer the type of any variable and flags errors that it finds. You don't have to type check an entire program as the Flow program will only check files that start with /*@flow*/.

Notice that Flow is a standalone program that you feed your JavaScript files into and see the errors in the output. 

For example, if you have the  program

/* @flow */
function foo(x) {
  return x * 10;
foo('Hello, world!');


then Flow will notice that the input parameter x has to be a number for the call to work and generates:

hello.js:5:5,19: string
This type is incompatible with
  hello.js:3:10,15: number


The problem is that this sort of type inference quickly starts to run into difficulties caused by ambiguous code. So, in addition, Flow lets you add type annotations. For example:

/* @flow */
function foo(x: string, y: number): string {
  return x.length * y;


This makes it easier to type check and you can make things more sophisticated by not only type checking base types but nested class types. Some of the ways that Flow uses type annotations go beyond a simple class hierarchy, e.g. it tries to account for properties that are dynamically added to objects. 

The problem is that adding type checking annotations like this makes for illegal JavaScript. To solve this problem you have to run the JavaScript through an annotation removing program - either offline or in the browser.

This is effectively turning JavaScript into another language. This is the approach used by other "type checked" JavaScripts like  Microsoft's TypeScript. A better plan would be to somehow make the type annotations part of a comment, but to date no way of doing this in a flexible way has been invented. How, for example, do you use comments to type parameters in a function?

Flow has a lot of what could be called ad-hoc typing rules that are designed to help find the sort of bugs JavaScript is prone to.

For example, Flow will detect a null value and complain if you try to use it in a way that would generate a runtime error. If, however, you add an if statement that checks for a null before using the variable, Flow knows that the code is safe. 

Flow does seem to perform useful checks on your code even without type annotation, but there is a warning in the documentation:

"Making existing code typecheck with Flow is not for the faint of heart - and often it may not be worth the effort in the short term. If your project just depends on a library, check out our guide on using Flow with external dependencies. Flow supports interface files so you can use libraries in a typed way without having to run Flow on them at all.

Why is typechecking existing code so hard? Libraries not written with types in mind often contain complex, highly dynamic code that confuses analyses such as Flow."

If you find that you are swamped by type errors when you check your code the advice is to run Flow on a less strict setting.

Flow is used by Facebook in production settings, but it is also very much a work in progress. You can help out on GitHub with improving the code and there are plans to make it better in the future by adding:

  • Support for converting existing TypeScript declaration files for common libraries on DefinitelyTyped.org to Flow declarations

  • ES6 module support

  • Compiling the Flow checker to JavaScript using js_of_ocaml

  • Powerful types to precisely express framework APIs that make heavy internal use of common reflection patterns

  • Editor integration for Flow type errors

  • Flow commands to support IDEs, such as autocomplete

  • Error message categorization and filtering by file

  • Improved errors: better blame attribution and error traces

  • A bunch of type system features like bounded polymorphism, enums, function purity analysis and lots more.




Golang Back In TIOBE Top 10

Google's system language Go is ranked #8 in the TIOBE Index for February 2024. This is the third time it has entered the Top 10. However, it is now in the highest position it has ever had to date.

EU Cyber Resilience Act Reduces Python Risk

The European Union has revised the Cyber Resilience Act (CRA), reducing the concerns of the open-source community.

More News


raspberry pi books



or email your comment to: comments@i-programmer.info





Last Updated ( Thursday, 20 November 2014 )