flowtypeflow-typed

how to publish a flow-typed library with its own types


I've written a index.js module in flow which I transpiled with Babel, placed within a /dist directory and defined as the main file in the package.json:

{
  "name": "my-lib",
  "version": "0.9.0",
  "description": "...",
  "main": "dist/index.js",
...
}

the dist directory only contains the transpiled file.

when I use it on another project, by importing via npm or yarn (yarn add <my-local-path-to-the-module>), I can import into my project's modules but I lose the original flow definitions (of course, babel stripped them). How could I associate the flow definitions along with the transpiled file so as to have the module as well as its Flow types ready to use?


Solution

  • After creating your dist files, you need to create associating declaration files from your source code. https://flow.org/en/docs/declarations/

    So in the end your dist dir would have a file structure like

    - dist
        - index.js
        - index.flow.js
        - something.js
        - something.flow.js
        - dir1
            file1.js
            file1.flow.js
    

    You can use either https://github.com/Macil/flow-copy-source or https://github.com/lessmess-dev/gen-flow-files