Syntax to pick named exports from a module

# Gray Zhang (8 years ago)

In many cases we need some named exports from a module, but currently there is no syntax to support things like:

import {flatMap, uniq} as _ form 'lodash';

Currently supported syntax could be:

import * as tmp from 'lodash'; let _ = {flatMap: tmp.flatMap, uniq: tmp.uniq};

There are reasons for me to downvote this syntax:

  1. It causes extra lines of code without improving readability
  2. There is no object property pick syntax so I’m required to write redundant code
  3. The let statement is not forced to be static so it is not that easy for tree shaking, once a developer use dynamic statements in the let line it breaks tree shaking

or we may code like this:

import {flatMap, uniq} from 'lodash'; let _ = {flatMap, uniq};

Still it comes with some shortcomings:

  1. It declares too many variables, variables which only used once is not a good smell
  2. It cost more efforts to adding or removing selected exports, it could fail if I just add a named export in import statement but forget to add it in the let line

In this case, I think a import {x, y} as z from 'some' syntax can help, and it doesn’t seem to conflict with any existing sytnax