DEV Community

Discussion on: How to add Barrels in TypeScript (or JavaScript)

Collapse
 
macsikora profile image
Pragmatic Maciej

You should also add - don't do that ;)

Collapse
 
luispa profile image
Info Comment hidden by post author - thread only visible in this permalink
LuisPa

Hi! What did you mean?

Collapse
 
macsikora profile image
Pragmatic Maciej

Tell me why I should maintain one + file for every directory and what is an issue to import file directly? And who puts one interface per file?

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

Probably more prevalent in declarative styles of codebase? I would see one or more varients in one file. But not just one interface.

Collapse
 
fjones profile image
FJones • Edited

Sorta. Don't do that... with a blanket export.

Having an index barrel with all available exports can be very useful for readability and makes it easier to move/rename the implementation files. (Think __init__.py indexes in python.) It also allows for implicit private exports which can be more easily respected by a module consumer.

That said, export * from 'foo' introduces magic that may in fact produce more opaque code instead. Prefer explicitly exporting the relevant symbols as an actual index.

Collapse
 
boywithsilverwings profile image
Agney Menon

I do believe the barrel pattern is useful in some cases. Like say, an API interface, so you can call the APIs with something like api.user.get, so user gets autocomplete on the APIs can narrow down from there. But putting interfaces/components there in the same name can get confusing real quick. With editors like VS Code that support automatic imports, all users would see two imports for this and then might end up picking the wrong one making the barrels kinda useless.

Some comments have been hidden by the post's author - find out more