pgreen2
pgreen2

Reputation: 3651

How can I use namespace import without importing everything?

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()

Upvotes: 0

Views: 228

Answers (1)

Knaģis
Knaģis

Reputation: 21485

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

import { pluck, delay } from 'lodash';

const _ = { pluck, delay };

Upvotes: 1

Related Questions