In Nov. 2014, 3 months ago, Facebook open-sourced a new mand 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)
In Nov. 2014, 3 months ago, Facebook open-sourced a new mand 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)
Share Improve this question edited Feb 18, 2015 at 17:31 danvk 17k8 gold badges79 silver badges133 bronze badges asked Feb 3, 2015 at 10:01 knbknb 9,4535 gold badges66 silver badges87 bronze badges 1- 1 On their future plans page, Facebook says they intend to add "Support for converting existing TypeScript declaration files (.d.ts) for mon libraries on DefinitelyTyped to Flow declarations." This would be a great solution, but I don't see any signs that it's happened yet. – danvk Commented Feb 18, 2015 at 17:32
2 Answers
Reset to default 4This 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./solnetdigital/flow-interfaces-jquery/blob/master/interfaces/jquery.js
If you want an actual jQuery flow interface definition, you can look at the one provided in https://github./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.