Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between import * as & import { default as }

I have the following imports:

import { default as service } from "../service";

VS

import * as service from "../service";

My service is exported like so

module.exports = {

    init(store) {
         _store = store;
    },

    beginPayment() {
    }

};

I would expect that only the second import would work since there is no export default, however both seem to work.

What is the difference between these? Is one preferred over the other?

If this is a duplicate I apologize, I didn't find anything specific to my example on SO or Google.

like image 243
Brandon McAlees Avatar asked Nov 20 '18 15:11

Brandon McAlees


1 Answers

If you are importing the default, there has to be a default.

In general, the community appears wary of default exports at the moment as they seem to be less discoverable (I have no specific citation, but I've watched the conversation!)

If you are working in a team, whatever they say is the correct answer, of course!

So without a default, you need to use:

import * as service from "../service";

Or choose a specific thing:

import { specificNamedThing } from "../service";
like image 159
Fenton Avatar answered Sep 22 '22 12:09

Fenton