javascript - Flowtype: package for both typed and untyped users? -


i have shared npm package business logic consumed several js apps. there es6+flow sources files in src , built vanilla js files in lib, , package's main set lib/index.js, exports things should visible.

i spent few days rewriting package using flow types, neatened of code , helped detect problems. works great, type checks, , tests pass. babel removes flow bits clients require package still plain files in js , work fine.

but allow clients of package (optionally) type signatures of functions , check calls correctly typed. could write declaration file, seems silly since types in original pre-babel source code. @ same time, can't provide source because clients may not using flow or babel. , i'd rather not have people want types having know exact source file path , have require('my-package/src/the-piece-i-want')

is there standard way write package flow require('my-package') just works both plain node users , type-checks babel+flow users? if not, other people problem doing?

there is! check out blog post: http://flowtype.org/blog/2015/12/01/version-0.19.0.html#declaration-files

when publish package, leave copy of original source (uncompiled) right next compiled version of file extension of .js.flow.

so like:

/package.json /src/index.js          <-- compiled version /src/index.js.flow     <-- original version /src/lib/mylib.js      <-- compiled /src/lib/mylib.js.flow <-- original 

when flow sees .js.flow file same name adjacent .js file, use former shadow latter.


Comments