13
import {Component, ...actions} from '../MyModule';

Seems to be throwing a linting error. Is there a reason why you can't "spread" on an ES6 import statement?

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
majorBummer
  • 6,357
  • 5
  • 29
  • 44
  • What do you expect the destructuring to do? Give you all other exports minus `Component`? – nils Mar 08 '16 at 09:53
  • 1
    Have a look at the spec: http://www.ecma-international.org/ecma-262/6.0/#sec-imports the notation for _NamedImports_ may look like object notation but it is something different (that is why there is no destructuring). If _MyModule_ exports an object you could do the destructuring in an assignment. – Matthisk Mar 08 '16 at 10:27
  • 1
    Mostly because spread operators are not part of ES6, but a proposal for some future version. – Bergi Mar 08 '16 at 17:51

1 Answers1

22

ES6 import syntax is not destructuring, it's as simple as that. The syntax starts with {, but its format is entirely different, and the way that it is handled in the implementation is quite different. For instance, you can rename imports with

 import {Component as MyComponent} from './MyModule';

which is clearly not an object literal.

If you need an object that you can use to reference imports as properties, you could do

 import * as MyModule from '../MyModule';

then use MyModule.<exportName>. If you objective is specifically to get an object that contains all of the export values, excluding Component then you could always do destructuring after, e.g.

 const {Component, ...actions} = MyModule;
loganfsmyth
  • 135,356
  • 25
  • 296
  • 231