Flow-typed - Generate Libdef
Asked Answered
F

1

8

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://mcmap.net/q/1374385/-get-type-definitions-from-imported-files-not-marked-with-flow

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.

Forcier answered 5/4, 2017 at 15:16 Comment(0)
G
2

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.
Genniegennifer answered 29/5, 2017 at 9:10 Comment(1)
But what do you do, if you have a bundled package? I rollup the code to make require faster (1 mid size file vs. 300 littles files), but then copying the source files as *.js.flow won't work as there is no corresponding *.js file anymore, or will it?Rhys

© 2022 - 2024 — McMap. All rights reserved.