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 import
s.
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";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With