Search code examples
javascriptflowtype

What is the "types first" Flow architecture?


A blog post by the Flow team describes a "re-architecture" of Flow called "types-first". As far as I can tell, the only description is in this quote from the blog post:

"...it exploits full type annotations at file boundaries to perform better (more parallelizable and less redundant) separate compilation."

Is there more detail about this anywhere? Specifically, I'm wondering what these full annotations are: what are the new restrictions on source code and declaration files?

For example, is this allowed?

import { func } from "./other-module";
export const myNumber = func(num1, num2);

It's problematic in TypeScript, since the type of myNumber is impossible to resolve without knowing the type of func. Will the "types-first" re-architecture of Flow require users to write:

import { func } from "./other-module";
export const myNumber: number = func(num1, num2);

This is just one specific question I have. What I'm looking for is a little bit more information and a link to a document explaining all the known implications of the re-architecture.


Solution

  • It sounds really flashy and maybe it is under the hood. But in reality it's quite simple. In your code snippet you're absolutely correct, it pretty much means just that.

    You must have an explicitly defined type before you export

    Though not necessarily right before you export. The following works too.

    const TestComponent = (): React.Node => {};
    
    export default TestComponent;
    

    It adds a little more overhead, but the benefits are:

    • improvements in performance because flow doesn't need to scan all dependencies before it can give you soundness checks
    • More reliable code because flow runs within module boundaries so you don't get flow errors that are caused by deeply nested dependencies.

    They've also released a new blog post that goes into this further since types first have now been released officially. https://medium.com/flow-type/types-first-a-scalable-new-architecture-for-flow-3d8c7ba1d4eb

    UPDATE The types-first architecture is now documented.