Marcus Junius Brutus
Marcus Junius Brutus

Reputation: 27306

how to import flow annotations, types and interfaces from an npm-published module

I have verified through testing that two patterns are possible for importing flow annotations, types and interfaces from an npm-published module.

In what follows I 'm using the below module names:

pattern 1

module A

module B

pattern 2

module A

module B

The above two patterns are the only methods I have discovered and have verified that they work (though I could find no comprehensive write-up).

My questions are:

  1. which pattern is more idiomatic / advisable ?
  2. is there another pattern ?

Regarding question #1 I can see that "pattern 2" is the only possible method if "module-A" is published by another organization / person. Otherwise if one publishes both modules I think "pattern 1" is more straightforward.

Upvotes: 10

Views: 4721

Answers (1)

ryyppy
ryyppy

Reputation: 442

You are absolutely right with your observations. These are the two methods to publish your flowtype definitions. There is no other way to do it, or better: The official migration plans will kinda go in both directions, because for now, it is impossible to force all JS projects to adapt flow.

Right now, "pattern 2" describes what we call 'libdef' files or 'declaration files'. With the flow-typed project, we try to vendor high-quality libdef files for common third-party node modules.

Both patterns have their up- and downsides... the biggest problem with vendoring *.flow.js files is, that they assume a specific version of flow (e.g. peerDependency flow-bin doesn't understand your newest used syntax). On the other hand it is a leaner way to keep the types synced up with the actual codebase.

Anyways, both ways are valid, just make sure your consumers are not forced to change their flow version just for your package.

Some another useful information:

[email protected] introduces an experimental new feature called flow gen-flow-files, which will create your *.flow.js in a more efficient manner by just copying type information instead the whole code.

Also, I created an issue on flow-typed which starts the exact same discussion right here: https://github.com/flowtype/flow-typed/issues/286

Cheers

Upvotes: 10

Related Questions