How to simplify and organize imports in Typescript

Sometimes we have a long list of imports, with files that come from the same place, it makes our code noisy and a bit longer, something like:
import { BeersService } from './services/beers.service';
import { WhiskyService } from './services/whiski.service';
import { WineService } from './services/wine.service';
We can simplify it by exposing all files, from a single file, to point to all of them.
Create drinks.ts into the service directory and export all services.
export * from './beers.service';
export * from './whiski.service';
export * from './wine.service';Now we can update our files, to the new path.
import { BeersService, WhiskyService, WineService } from './services/drinks';Thanks @lissetteibnz , If rename the filename from drinks.ts to index.ts, Javascript understand the file index like the entrypoint for the directory, so it works only using the directory name.
import { BeersService, WhiskyService, WineService } from './services';The code looks clean and easy to ready because all of them comes from the same place.
Photo by Marcin Jozwiak on Unsplash
Related Articles
Getter and Setter with Typescript
The encapsulation is an essential part of OOP, and Typescript support gets and sets keywords for members of classes. The encapsulation can be used with the get and set keywords before a function name, then using it. We can encapsulate some logic and ...
When Use Arrays, Tuples, Maps, and Sets In Typescript with Examples
A few days ago, a friend asked how to prevent duplicate keys in an array, and I told him there are other collections to work with, and each one is suited for a specific situation. Because I love the NBA, I tried to find an easy way to explain each da...
Real Software. Real Lessons.
I share the lessons I learned the hard way, so you can either avoid them or be ready when they happen.
Join 13,800+ developers and readers.
No spam ever. Unsubscribe at any time.