lodash / babel-plugin-lodash

Modular Lodash builds without the hassle.
Other
1.95k stars 95 forks source link

Error when importing flow types #153

Closed dlindenkreuz closed 7 years ago

dlindenkreuz commented 7 years ago

When using the Flow type definitions for lodash from flow-typed/npm, webpack builds fail with the following error message:

ERROR in ./src/utils/findCountry.js
Module build failed: SyntaxError: The 'lodash' method `Predicate` is not a known module.
Please report bugs to https://github.com/lodash/babel-plugin-lodash/issues.

   8 |   translations: { [string]: string }
   9 | }
> 10 | export function findCountry(predicate: Predicate<*>) {
     |                                        ^
  11 |   return find(Countries, predicate)
  12 | }
  13 | 

findCountry.js:

// @flow
import { deburr, camelCase, find } from "lodash"
import type { Predicate } from "lodash"
import Countries from "../../data/countries.json"

type Country = {
  name: string,
  translations: { [string]: string }
}
export function findCountry(predicate: Predicate<*>) {
  return find(Countries, predicate)
}

NB: I use babel-plugin-transform-flow-strip-types.

jdalton commented 7 years ago

What's import type { Predicate } from "lodash" about?

dlindenkreuz commented 7 years ago

That statement imports the type definition from the community-maintained flow-typed repository, in particular from this file: https://github.com/flowtype/flow-typed/blob/master/definitions/npm/lodash_v4.x.x/flow_v0.28.x-/lodash_v4.x.x.js

jdalton commented 7 years ago

It looks like it's coming from the "lodash" package though. How can I distinguish between the two? (I'm not a flow type user so you'll have to walk me through it).

dlindenkreuz commented 7 years ago

Lodash type definitions become implicitly available for the lodash module through the declare module "lodash" statement inside of the flow type declaration file.

This works automatically (without import type { foo } from "lodash" in the user's code) for lodash's own methods because the definitions are exported here explicitly. However, I need to use the Predicate type which is declared as part of the module, but not in the "default" export of the flow definition file.

Hence my named import statement which in turn causes trouble with babel-plugin-lodash. Flow is happy with this and checks my types correctly.

You can find more information about the implicit availability of type declarations in the official Flow docs (check out the last section for a specific lodash example).

jdalton commented 7 years ago

I'll see if Babel makes a distinction in its AST. If not then you're out of luck.

Update: Patched :tada: :tada: