Olle Härstedt
Olle Härstedt

Reputation: 4020

Can I declare globals to Flow in the same file?

Is it possible to declare a global variable in the same file, and then analyze only this file? Similar like using eslint or jshint. Use-case would be adding jQuery, $, as an object with whatever properties.

Something like:

// @declare var $: object
let foo = $('.foo-elements');

Like in this link, but on the fly: https://flow.org/en/docs/libdefs/creation/#toc-declaring-a-global-variable

declare var PI: number;

I'm not using Babel, so the code has to be ES8 JS, browser-ready.

The documentation mentions inline declarations, but they are not JS and must be transpiled to work, it seems: https://flow.org/en/docs/declarations/#toc-inlining-declarations-in-regular-code

Upvotes: 0

Views: 69

Answers (1)

Brianzchen
Brianzchen

Reputation: 1335

So you want to declare a global variable using flow so that you do not get flow errors. But you do not use babel or and transpiler so it must run without tooling.

I think comment syntax is what you're looking for https://flow.org/en/docs/types/comments/

// @flow

/*::
declare var $: {
  (...args: Array<any>): any;
  [key: string]: () => any,
  ...
},
*/

let foo = $('.foo-elements');

Taken the liberty to also add the most basic implementation for $ object

Upvotes: 1

Related Questions