Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"JQuery Interface file" for Flow (Static Type Checker for JavaScript from Facebook)?

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)

like image 230
knb Avatar asked Feb 03 '15 10:02

knb


2 Answers

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

like image 163
Ankush Dharkar Avatar answered Sep 29 '22 04:09

Ankush Dharkar


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.

like image 25
IanVS Avatar answered Sep 29 '22 03:09

IanVS