Skip to content

Instantly share code, notes, and snippets.

@karlhorky
Created July 31, 2018 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlhorky/ee796f024d6f4c4dffe823fa6bf71a8d to your computer and use it in GitHub Desktop.
Save karlhorky/ee796f024d6f4c4dffe823fa6bf71a8d to your computer and use it in GitHub Desktop.
Check Flow types without @flow pragma comment

Originally from: facebook/flow#284 (comment)

I use following solution which work great:

  1. .flowconfig file is placed to src folder.
  2. node_modules and flow-typed folders resides in root folder.
  3. Option all=true is enabled in .flowconfig file.
  4. flow-typed typings is included in .flowconfig file.
  5. Well-typed modules from node_modules folder that contain module own typings is included to .flowconfig file manually.

So .flowconfig file look like that:

[include]
../node_modules/mobx
../node_modules/other-well-typed-module-with-typings

[libs]
../flow-typed/npm
../flow-typed-generated-stubs/npm

[ignore]
# Nothing ignored

[options]
all=true

As a result:

  1. I do not need annotate any file with //@flow header. All my files are type-checked by default. So there are no more mess with forgotten headers and untyped files.
  2. Type declatations from flow-typed repository is supported. They are placed to flow-typed folder and can be downloaded automatically on each build.
  3. Well-typed modules that includes module own typings is also supported. Path to such modules is included in .flowconfig file.
  4. flow-typed declarations stubs is used for bad-typed modules. Generated or created declarations is placed to flow-typed-generated-stubs. So bad-typed modules do not affect type checking.
  5. No overhead on type-checking entire node_modules directory.
  6. I do not ignore anything by default. And that is good because in future I can use [ignore] section for excluding some module js-files if only some of module js-files is well-typed and other is bad-typed.

There are one disadvantage of described solution. I need to include all well-typed modules (in node_modules folder) to my .flowconfig file. It is not a big issue because there are only a few well-typed modules currently.

P.S. Do not try specifying more exact path for well-typed modules. For example, including ../node_modules/mobx/lib instead of ../node_modules/mobx is bad idea. It will not work. Currently I do not know the reason of that behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment