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….


This content originally appeared on DEV Community and was authored by Dany Paredes

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 index.ts, (you can name as you want, but most common is index) 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/index';

The code looks clean and easy to ready because all of them comes from the same place.

Photo by Marcin Jozwiak on Unsplash


This content originally appeared on DEV Community and was authored by Dany Paredes


Print Share Comment Cite Upload Translate Updates
APA

Dany Paredes | Sciencx (2021-10-16T13:53:19+00:00) How to simplify and organize imports in Typescript. Retrieved from https://www.scien.cx/2021/10/16/how-to-simplify-and-organize-imports-in-typescript/

MLA
" » How to simplify and organize imports in Typescript." Dany Paredes | Sciencx - Saturday October 16, 2021, https://www.scien.cx/2021/10/16/how-to-simplify-and-organize-imports-in-typescript/
HARVARD
Dany Paredes | Sciencx Saturday October 16, 2021 » How to simplify and organize imports in Typescript., viewed ,<https://www.scien.cx/2021/10/16/how-to-simplify-and-organize-imports-in-typescript/>
VANCOUVER
Dany Paredes | Sciencx - » How to simplify and organize imports in Typescript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/16/how-to-simplify-and-organize-imports-in-typescript/
CHICAGO
" » How to simplify and organize imports in Typescript." Dany Paredes | Sciencx - Accessed . https://www.scien.cx/2021/10/16/how-to-simplify-and-organize-imports-in-typescript/
IEEE
" » How to simplify and organize imports in Typescript." Dany Paredes | Sciencx [Online]. Available: https://www.scien.cx/2021/10/16/how-to-simplify-and-organize-imports-in-typescript/. [Accessed: ]
rf:citation
» How to simplify and organize imports in Typescript | Dany Paredes | Sciencx | https://www.scien.cx/2021/10/16/how-to-simplify-and-organize-imports-in-typescript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.