0

We use lodash in an angular app written with typescript. Currently we import lodash as follows:

import * as _ from 'lodash';

//.. code which uses _.pluck()

However, for the sake of tree shaking, we want to change to the following:

import {pluck, delay} from 'lodash';

//.. code which uses _.pluck() needs changed to pluck()

The problem is we need to do a lot of tedious code changes because using the second import method loses the namespace of _ and there could be name conflicts. Is there a way of specifying the things we want to import but maintain the namespace? I was thinking something as follows, but it doesn't work:

import {pluck, delay} as _ from 'lodash';

//.. code which uses _.pluck() needs changed to pluck()
pgreen2
  • 3,601
  • 3
  • 32
  • 59
  • Tree shaking works just fine with `import * as _ from 'lodash';`, [there is no difference](https://stackoverflow.com/a/45746950/1048572) – Bergi Dec 08 '21 at 15:07

1 Answers1

1

Not directly in import but you can create the _ object manually.

import { pluck, delay } from 'lodash';

const _ = { pluck, delay };
Knaģis
  • 20,827
  • 7
  • 66
  • 80