Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: import one and the rest?

For object const a = {b: 1, c: 2, d: 3}, we can do destructuring like this: const {b, ...rest} = a.

I'm wondering if it's also possible for imports.

Say I have a file file1.js:

// file1.js
export const a = 1;
export const b = 2;
export const c = 3;

Can I import from this file by importing one and the rest like destructuring?

// file2.js
import {a, ...rest} from "file1";
like image 862
CSSer Avatar asked Sep 12 '20 13:09

CSSer


1 Answers

Not directly, there's no import form for it.

What you can do instead is import the module's module namespace object, then use destructuring on it:

import * as file1 from "file1";
const {a, ...rest} = file1;

FWIW, the specification has an example list of import forms here. It doesn't show what you can and can't combine, but those are the basic forms.

like image 140
T.J. Crowder Avatar answered Oct 21 '22 06:10

T.J. Crowder