8

Is this valid javascript? It does not error, and appears to work.

export {default as Chooser} from "./chooser";

My interpretation is:

  1. import the default from "./chooser"
  2. export the result from #1 as Chooser

Is this what is happening?

mziemer
  • 338
  • 3
  • 13

2 Answers2

3

Is this valid JavaScript?

Yes.

Is this what is happening?

Yes.

0

Your interpretation is correct.

import the default from "./chooser"

This is correct. The default thing being exported is Chooser and on import, you must use the name given to it with as ...:

import { Chooser } from "./chooser";

export the result from #1 as Chooser

This is also correct. The name Chooser is giving the default a new name and exporting it.


Let me break this down:

export {
    default as Chooser
} from "./chooser";

What this does is specify the file from which it is exported, and default as Chooser exports the default under the name Chooser. Now, on import:

import { Chooser } from "./chooser";

You must specify Chooser to import because you've essentially named the default.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • Really? *This is also correct, but the module exports Chooser as default, and then on import, import finds the default export and imports it.* I think the module exports it under the name `Chooser`, and it must be imported as `import {Chooser} from './exports';`. If you want to re-export the default export, I think you need to say `export {default} from './chooser`;`. Also, what does `foo` in your answer refer to? –  Sep 09 '16 at 05:59
  • 2
    *"can be used globally as its the default thing being exported"* What does default exports have to do with "globally"? What does that mean in that context? – Felix Kling Sep 09 '16 at 06:10
  • @FelixKling I'm not sure *why* I put that in but it's removed. Thank you for pointing that out! – Andrew Li Sep 09 '16 at 12:48
  • @torazaburo I have edited the answer, thanks for the constructive criticism! – Andrew Li Sep 09 '16 at 12:50