r/javascript • • Nov 29 '17

Why we have banned default exports in JavaScript and you should do the same

https://javascript.works-hub.com/blog/Why-we-have-banned-default-exports-in-JavaScript-and-you-should-do-the-same?utm_source=eliott&utm_medium=reddit&utm_campaign=content
0 Upvotes

10 comments sorted by

6

u/kingdaro .find(meaning => of('life')) // eslint-disable-line Nov 30 '17

The title seems a little dogmatic, but I definitely agree, especially in relation to tooling with TypeScript. Project-wide renames sometimes work with default exports, but not as consistently as named exports. Relevant link

3

u/hirako2000 Nov 30 '17

If default exports work for some teams.. not sure why we should all do the same. but thanks for sharing...

2

u/IDCh Nov 30 '17

Hey come on I have my entities with default export wtf. (judging by the title)

3

u/beaverusiv Nov 30 '17

I had not used default exports before (as I just inherently knew the benefits of named ones) and stepping into a project that has everything exported default instead of named brought me to the same conclusion - never use default.

1

u/burtgummer45 Nov 30 '17

We better get to banning this also.

import { rightName as wrongName } from module

8

u/kingdaro .find(meaning => of('life')) // eslint-disable-line Nov 30 '17

I don't agree. There are cases where different files may have similarly-named exports being imported into one file. Cases like this are probably what as newName is meant for. Example:

import { combineReducers } from 'redux'
import { reducer as userReducer } from 'modules/user'
import { reducer as authReducer } from 'modules/auth'
import { reducer as chatReducer } from 'modules/chat'

export const rootReducer = combineReducers({
  user: userReducer,
  auth: authReducer,
  chat: chatReducer,
})

A somewhat contrived example, and a little boilerplatey, but you get the idea.

7

u/burtgummer45 Nov 30 '17

Well I don't agree either, I think the original post is dumb, I was making a point that named exports can be whatever name you want.

3

u/kingdaro .find(meaning => of('life')) // eslint-disable-line Nov 30 '17

Ah, my bad then :D

0

u/[deleted] Nov 30 '17

[deleted]

1

u/kingdaro .find(meaning => of('life')) // eslint-disable-line Nov 30 '17

There are better ways of handling this specific case; it's only an example. OP wasn't being serious anyway.