Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow-typed - Generate Libdef

I'm using Flow to help author a JS project. If I want to provide a libdef file to supplement it do I need to create it manually, or am I able to execute some magic command that I'm not aware of yet which will generate the lib def for me?

Something like $ flow-typed doyourmagic would be nice.

EDIT:

Found this https://stackoverflow.com/a/38906578/192999

Which says:

There's two things:

If the file is owned by you (i.e. not a third party lib inside node_modules or such), then you can create a *.js.flow file next to it that documents its exports.

If the file is not owned by you (i.e. third party lib inside node_modules or such), then you can create a libdef file inside flow-typed/name-of-library.js

For .js.flow files you write the definitions like this:

// @flow 
declare module.exports: { ... } 

For libdef files you write the definitions like this:

declare module "my-third-party-library" {   declare module.exports: {... } }

For my question I fall into the "is owned by you" camp.

I guess I'm confused as to:

  1. How I write these files.
  2. How/where I publish these files to package it up for another project to reference.

Also, why do I need to create the .js.flow file manually? Can this not be magically generated? Perhaps that's the intention going forward but not implemented yet.

like image 808
ETFairfax Avatar asked Apr 05 '17 15:04

ETFairfax


1 Answers

I found a nice guide showing how to package flow code together with the compiled code. So:

  1. You do not have to write your own libdefs, you can use the entire flow source code. If you want a definition with only the type declarations, you can look into flow gen-flow-files, although that is still experimental and might fail.
  2. You can package them as *.js.flow and the flow checker will automatically pick those up when you import your library.
like image 126
Sander Avatar answered Sep 19 '22 08:09

Sander