BP69: Use Index.ts to simplify imports

Index.ts allows you to organize your importsThis file can be used to import multiple modules from other folders and re-export them so that they can be consumed by the other modules more easily.

The modules that were re-exported in Index.ts can be then imported from Index.ts and you don't need to specify the exact original location to each module.

As components are usually used and imported in Feature module and respective  Router module, the import is duplicated and the path to a module is stored in two places. If you later need to change the path of the file, you have to change it in two places. However, if the Index.ts is used, the change needs to be done only once.

Pros

  • More readable imports with shorter parths
  • Paths are stored only in Index.ts so when files are moved to a different location, fixing paths is simple
  • If the original module gets moved to a different location, you can change just the Index.ts and the dependent modules don't need to be updated.

Cons

  • Can lead to circular dependencies

Comments

Jan
I really like how they help you to eliminate paths from multiple places in the code. You have paths just in Index.ts and everywhere else you use just path to index.ts.

Download Better Coder application to your phone and get unlimited access to the collection of enterprise best practices.

Get it on Google Play