In modern ES code, I love the named imports & exports since it allows us to only import what we need. Tooling has jumped on this as well by stripping out any exports that aren't used (treeshaking).

However, a big downside of named imports is that you lose any "namespacing", which is especially relevant for generic function/variable names. For example, `lodash` and `async` both export a `map` function, and it isn't unlikely to use both packages in a single project, or even a single module. This gives naming conflicts.

We currently have 2 options to resolve these conflicts:

- Alias individual imports:

    import { map as lodashMap } from 'lodash';
    import { map as asyncMap } from 'async';

- Replace with * import

    import * as _ from 'lodash';
    import * as async from 'async';

Neither option is ideal:

- Aliasing results in either inconsistent code (some imports are prefixed, others aren't) or "smurfnaming". Prefixing is often also less readable than namespacing, especially for longer names (`lodashLastIndexOf` vs `lodash.lastIndexOf`).
- Star imports prevent treeshaking.

The solution to this seems simple enough: allow a group of named imports to be aliased as a whole:

    import { map } as _ from 'lodash';
    _.map();

This preserves both "namespacing" and selective importing, giving the best of both worlds.

To me this seems like such a no-brainer that I'm actually surprised that this isn't possible already. Has something like this been considered at some point? And what would be the downsides of this? Or is there just too little interest in this?
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to