Shivanshu Goyal
Shivanshu Goyal

Reputation: 1452

Using Flow for browser apps

I want to use Flow (the static type checker for JavaScript by Facebook) for browser apps. How do you get Flow to follow the other .js files which are being used by a given .js file? In Node.js, the use of the require function makes Flow follow other modules and check for type errors, and I want a similar functionality for browser apps too.

Say I have a Classroom.js file which uses a module Student.js. When I run Flow, it will throw the error identifier Student Unknown global name.

Upvotes: 2

Views: 830

Answers (2)

danvk
danvk

Reputation: 16905

Facebook uses browserify to do this in their Flux Chat example. Browserify inlines the require statements in node-style JavaScript to produce code that can be run in the browser.

Here are the relevant bits of their package.json:

{
  "scripts": {
    "build": "NODE_ENV=production browserify . | uglifyjs -cm > js/bundle.min.js",
  },
  "browserify": {
    "transform": [
      ["reactify", { "stripTypes": true }],
      "envify"
    ]
  }
}

Upvotes: 2

knb
knb

Reputation: 9295

I have run into this problem myself, just now.

As you have a single, simple dependency (Classroom --> Student, this link contains a workaround that might help in your case. http://flowtype.org/docs/third-party.html

It says, create a new directory, put an "interface file" inside (this is basically a stub. pick any name for the file), and include everything with

flow check --lib /abs/path/to/my/interfacefiledir/ Classroom.js 

For more complex scenarios, like secondary dependencies, third-party-libraries (like including jQuery with its full API), I don't have a strategy.

I think the facebook-flow team has put "add more interface files" on their to-do list. http://flowtype.org/docs/coming-soon.html#_

Upvotes: 0

Related Questions