ES Modules: import & destructure at once!

The Problem:

ES modules don't support for import and destructure the exports at once, in some situations, it takes an additional step like this:

import { config } from './config'
const {port, ...rest} = config

The solution:

Wouldn't it be nice if we could import and destructure in no time? something like this:

import { config: { port }, ...rest } from './config'

Thanks!

I don't think it would be nice, it would only increase the confusion about the difference between them. Notice that in a syntax like

import { config as { port, ...rest } } from './config';

it would not be clear whether port and rest are declared as let, const, or would become an alias (reference, getter) for config.port and, uh, something respectively. The extra line really doesn't hurt. If you have to do this a lot, consider whether the config module should rather have port and rest named exports in addition to config.

1 Like