How do I keep Flow annotations intact through Babel/Webpack?

947 views Asked by At

I have a private NPM module for utility functions with Flow type annotations.

I develop in Node v7 and, prior to npm publish, I use Babel/Webpack to transform it to earlier Node versions to run in environments like AWS Lambda.

I use transform-flow-strip-types plugin for Babel to compile, but as I understand it, that means I lose static type checking of my exported functions when I import the module into another project.

I tried babel-plugin-syntax-flow, but it throws unexpected token errors, so I'm assuming this isn't its intended use.

Can I transform my src/ with Babe while keeping Flow types intact?

The type annotations are simple (string, number, mostly), so I'd like to avoid writing typedefs to export with every function.

1

There are 1 answers

0
Erik Haider Forsén On BEST ANSWER

I came across an article explaining how to achieve what you are describing:

http://javascriptplayground.com/blog/2017/01/npm-flowjs-javascript/

By publishing both javascript where flow type is stripped together with the original flow typed file, you will get proper flow type checking when using your library.

This is achieved by publishing the original file as file.js.flow, and the babelified file as file.js.