lodash / babel-plugin-lodash

Modular Lodash builds without the hassle.
Other
1.96k stars 91 forks source link

Issues while using with Babel 7 and Typescript plugin #226

Closed RIP21 closed 5 years ago

RIP21 commented 5 years ago

If you import some Types from lodash like that. import { orderBy, Dictionary } from 'lodash'

babel-plugin-lodash throws an error.

// SyntaxError: /backoffice-frontend/src/containers/xxxx/xxxx/Calendar.tsx:
// The 'lodash' method `Dictionary` is not a known module.

Will be nice to handle somehow exported Types. Or just stripe them away, since they are not affecting compilation of babel-typescript-plugin since he doesn't care about types etc. He just outputs the JS code not looking at tsconfig.json or whatever. Or maybe ordering of plugins matter? Don't know.

jdalton commented 5 years ago

Hi @RIP21!

We translate the example import clause into var Dictionary = require('lodash/Dictionary'). If I recall there should be another way to import types other than the ESM syntax. If there is you should use the alternative syntax to import types.

RIP21 commented 5 years ago

That would be nice to have an example TBH :/ Because I think I will be not the last person to ask such a question. I wasn't able to find an answer tho.

RIP21 commented 5 years ago

I assume this syntax should work.

type MyType = import('mymodule').MyType;
const myValue: import('mymodule').MyType;
Pearce-Ropion commented 2 years ago

If anyone needs an explicit example for how to do this:

import { orderBy } from 'lodash';
import type { Dictionary } from 'lodash';

Notice the type keyword. That is the distinction that the plugin uses in determining what is a lodash function.