9

In Nov. 2014, 3 months ago, Facebook open-sourced a new command line tool, a static type checker called "Flow". Now I want to run it on a few of my older, existing javascript files. These contain references to the jQuery library.

My JS files were not written with static type-checking in mind. however, after including /* @flow */ at the top of the file, when I run flow with this Command:

flow myfile.js 

Result:

/var/www/myfilejs:70:12,17: identifier jQuery
Unknown global name
Found 1 error

As I understand it, the way to include jQuery into Flow's type checking process is to create an "interface file".

Has anyone done this yet for the jQuery library? (I use jQuery 1.9)

1
  • 1
    On their future plans page, Facebook says they intend to add "Support for converting existing TypeScript declaration files (.d.ts) for common libraries on DefinitelyTyped.org to Flow declarations." This would be a great solution, but I don't see any signs that it's happened yet.
    – danvk
    Feb 18, 2015 at 17:32

2 Answers 2

4

This is my interface file I like to call jQuery.js in a folder I call "flow_lib". This folder can be anywhere.

This is the code the jQuery.js contains for the interface declaration :

declare module "jQuery" {
    declare function $(obj: any): any;
}

var $ = require('$').$;

In your .flowconfig, include the folder like this :

[ignore]

[include]

[libs]
<path-to-folder>/flow_lib

[options]

Note : This method does not check for actual jQuery specifications. It's just a quick fix to get rid of the flow warnings and errors related to $ in your code. If you want to be more specific, use something like : https://github.com/solnetdigital/flow-interfaces-jquery/blob/master/interfaces/jquery.js

1

If you want an actual jQuery flow interface definition, you can look at the one provided in https://github.com/marudor/flowInterfaces.

Install it with:

npm install --save-dev iflow-jquery

or

yarn add --dev iflow-jquery

And then add the following to your .flowconfig file:

[libs]
node_modules/iflow-jquery/index.js.flow

After that, flow will infer the types of the parameters you provide to jquery functions, and will warn you if you are passing the incorrect types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.