Using flowtype in Meteor packages/applications


#1

I myself have never used flowtype before, but I’m considering trying to use it for Meteor packages/applications. Has anyone tried that? Does it work well?

And is it possible to support it through a meteor package? I can’t find any package doing that at the moment, but it would be nice if one can start using it simply by adding a package.


#2

I am following FlowType too, but from what I see so far, it is a lot of work to start using Flow on a big project with a lot of dependencies, especially Meteor due to a couple of reasons:

  • You need to annotate a lot of code that is dynamically generated in run-time
  • Or you need to convert the TypeScript definitions
    • Typescript def conversion is not perfect(?)
    • Meteor’s Typescript defs are out of date
  • Since Meteor has a different loading scheme, one would need to fork flow-type and write support for Meteor’s loading in OCaml

#3

You can just use it. It’s already included in the ecmascript package. See https://github.com/meteor/meteor/blob/devel/packages/ecmascript/README.md#syntax, hidden at the bottom :slight_smile:


#4

I got FlowType working with 1.3. Like @rahul said, you can already use it because it’s in the ecmascript package. But there are a few thing that you need to add to your .flowconfig so that it will understand the absolute paths in 1.3 and the meteor packages imports. Like these:

// Absolute path
import {Todos} from '/lib/collections/Todos.js';

// Meteor core package
import {Meteor} from 'meteor/meteor';

// Other atmosphere packages
import {thing} from 'meteor/author:packagename';

You solve this by adding these options to your .flowconfig:

module.name_mapper='^\/\(.*\)$' -> '<PROJECT_ROOT>/\1'
module.name_mapper='^meteor\/\(.*\):\(.*\)$' -> '<PROJECT_ROOT>/.meteor/local/build/programs/server/packages/\1_\2'
module.name_mapper='^meteor\/\(.*\)$' -> '<PROJECT_ROOT>/.meteor/local/build/programs/server/packages/\1'

This is the best setup I’ve come up with so far and it prevents you from having to add all kinds of definition files. I would love to have some feedback on this to hear if this is working for everybody.

More details can be found in this github issue (which is mostly about TypeScript: https://github.com/meteor/meteor/issues/6177


#5

@jamiter, did you have any problems with node_modules? I’m very new to flow. I tried to integrate flow into one of my projects and got tons of errors from node_modules. I looked it up on SO and couldn’t wrap my head around the declarations stuffs. Any help would be greatly appreciated! :wink:


#6

I hoped it would understand node_modules, but it is still an issue with flow. You can read about a workaround here: https://github.com/facebook/flow/issues/719

This is what they suggest when foo is a node module:

src/test.js

/* @flow */

import { Foo } from 'foo';

export default class Bar {
    foo : Foo,
    constructor() {
        this.foo = new Foo();
    }
}

lib/foo.js

declare module foo {
    declare class Foo {}
}

.flowconfig

[ignore]
.*/node_modules/*.

[include]
.*/src/.*

[libs]
lib/

This comes down to mocking all node modules you use… Which is horrible in my opinion. It doesn’t let you use node_modules as a library. I mean: It should find them to check YOUR code, but it shouldn’t check the node_modules themselves.

I’m going to chime in on the discussion at Facebook to get better node module support. For instance here: https://github.com/facebook/flow/issues/869

Edit:
It does seem like they did some work on this in the latest release (21 hours ago): https://github.com/facebook/flow/releases/tag/v0.22.0 Not sure if this helps us in any way yet :wink:


#7

Hi all! I have Flow running smooth for a small project. I wrote a blog post about what I had to do to get it working:

I plan to create a example repository where also issues can be reported. Coming Soon™.

(p.s. it requires Meteor 1.3.3+ and Flow 0.25+)


#8

This solution will only work for applications and not for packages.


#9

Correct… I ran into this as well. Haven’t found a workaround so far.

P.S. One of the biggest issues with FlowType is still how type errors from node_modules are handled. Thats the biggest turn off for most people.


#11

I just add “.*/node_modules/.*” to my flowconfig’s ignore section.

I no longer do this.


#12

It’s not a good idea to ignore whole node_modules - it’ll strip all non-flow-typed modules typings.

By the way: have you seen [Work in progress] Flowtype typings?