In Flow NPM packages, what's the proper way to

2019-09-16 15:19发布

问题:

If you use something like $FlowIssue it's not guaranteed to be in everyone's .flowconfig file. If you declare a library interface, that seems to only work for the given project, not in other projects that import your package (even if you provide the .flowconfig and interface files in your NPM package).

Here's the code I'm trying to suppress errors for in apps that use my package:

  // $FlowIssue
  const isSSRTest = process.env.NODE_ENV === 'test' // $FlowIssue
    && typeof CONFIG !== 'undefined' && CONFIG.isSSR

CONFIG is a global that exists when tests are run by Jest.

I previously had an interface declaration for CONFIG, but that wasn't honored in user applications--perhaps I'm missing a mechanism to make that work?? With this solution, at least there is a good chance that user's have the $FlowIssue suppression comment. It's still not good enough though.

What's the idiomatic solution here for packages built with Flow?

回答1:

Declaring a global variable

This is the way to declare a global variable: declare var CONFIG: any;. Instead of any you could/should use the actual type.

Error Suppression

With flow v0.33 they introduced this change:

suppress_comment now defaults to matching // $FlowFixMe if there are no suppress_comments listed in a .flowconfig

This means that there is a greater chance of your error being suppressed if you use $FlowFixMe.



回答2:

It doesn't appear that there is a standard. You would have to tell you used to create the suppress_comment in their config. Introducing a global way to do so doesn't appear a high priority to the flow team: https://github.com/facebook/flow/issues/662



回答3:

Differences in .flowconfig between your library and your consumers' code are a real issue, and there is no way to make it so that your code can be dropped into any other project and be sure it will typecheck. On top of that, even if you have identical .flowconfigs, you may be running different versions of Flow. Your code may typecheck in one version, but not in another, so it may be the case that consumers of your library will be pinned to a specific version of Flow if they want to avoid getting errors reported from your library.

Worse, if one library type checks only in one version of Flow, and another type checks only in another version, there may be no version of Flow that a consumer can choose in order to avoid errors.

The only way to solve this generally is to write library definition files and publish them to flow-typed. Unfortunately, this is currently a manual process because there is not yet any tooling that can take a project and generate library definitions for it. In the mean time, simply copying your source files to have the .js.flow extension before publishing will work in some cases, but it is not a general solution.

See also https://stackoverflow.com/a/43852211/901387