flow icon indicating copy to clipboard operation
flow copied to clipboard

Document .js.flow files

Open mvolkmann opened this issue 9 years ago • 15 comments

The old documentation described how you could omit types in a file called foo.js and declare the types in a file called foo.js.flow that is located in the same directory. I can't find that in the new documentation. Is that still a supported way to specify types?

mvolkmann avatar Mar 29 '17 15:03 mvolkmann

Yes, this is still supported.

samwgoldman avatar Mar 29 '17 23:03 samwgoldman

Can you comment on this? http://stackoverflow.com/questions/43082488/flow-declaration-files-getting-started#

mvolkmann avatar Mar 30 '17 02:03 mvolkmann

I've tried really hard to get a simple example of this to work and can't get it to detect a simple error where I pass a string for parameter that must be a number. Are you sure .js.flow are really working in the latest version of Flow? Here is my example:

math.js

function double(n) {
  return n * 2;
}

exports.double = double;

math.js.flow

//function double(n: number): number {} // doesn't work
declare export function double(n: number): number; // doesn't work

demo.js

// @flow
const math = require('./math');
console.log(math.double('bad')); // Flow should flag this, but doesn't

When I run flow in this directory, it says No errors!.

mvolkmann avatar May 08 '17 14:05 mvolkmann

I'm also not observing any enforcement by .js.flow files. Same sort of situation as @mvolkmann. Any help would be appreciated.

njgraf512 avatar Aug 10 '17 23:08 njgraf512

Very much would like this documented as well. Could inhibit adoption if we don't document this as it's one way to convince a team to transition to Flow without touching the JavaScript source.

AndersDJohnson avatar Nov 09 '17 05:11 AndersDJohnson

Having the same issue after using flow-copy-files. Also duplicated @mvolkmann's example and get no errors.

EDIT: The above example works if //@flow is added to the .js.flow file. flow-copy-source output does not work though.

shantp avatar Dec 01 '17 04:12 shantp

Using Flow 0.59.0, I can't figure out how to use .js.flow with a CommonJS export:

index.js.flow

declare export class Foo<T> {
  bar(value: T): T;
}

index.js

class Foo {
  bar(value) {
    return value
  }
}

module.exports = Foo

I've also tried adding module.exports = (require('./index'): typeof Foo) to index.js.flow.

aleclarson avatar Dec 26 '17 18:12 aleclarson

What I ended up doing was taking a Facebook open source project Draft JS (which auto-generates their .js.flow files) to figure out how they are formatted. If you clone and build the project and look in the "lib" directory, you'll see that the .js.flow files are just the raw js files with the flow syntax in them. I think flow just sees a .flow extension for a file corresponding to the one you add and it just type checks against that.

brenmcnamara avatar Jan 10 '18 02:01 brenmcnamara

@mvolkmann I believe math.js still needs the // @flow header for the entire thing to type check, otherwise it treats imports from the file as any which Flow ignores entirely.

LoganBarnett avatar Feb 13 '18 19:02 LoganBarnett

Assuming that's the error, I normally use this linter rule to catch misses like that.

LoganBarnett avatar Feb 13 '18 19:02 LoganBarnett

What's the status of this as of today (0.69) ? I'm just figuring out that the .flow files I have in my vendor libs are not taken in account at all. 🧐

dperetti avatar Apr 09 '18 13:04 dperetti

@dperetti https://github.com/renatorib/react-powerplug/blob/master/package.json#L15

TrySound avatar Apr 09 '18 14:04 TrySound

You the issue is most vendors don't ship their .flow files into /dist ? Looks like it could be the reason, indeed !

dperetti avatar Apr 09 '18 14:04 dperetti

@dperetti Sorry, what? Everything works fine with .flow in the projects.

TrySound avatar Apr 09 '18 14:04 TrySound

Doh! Found a culprit in my .flowconfig. Sorry !

dperetti avatar Apr 09 '18 14:04 dperetti