title: Splitting inversifyJS config file into multiple files
body: <p>I recently bumped into a backend project using Typescript and would like to implement IoC principle with the help of Inversify.js. Following the official documentation, I have one huge file named <code>inversify.config.ts</code> containing all my interfaces and classes that implement them:</p>
  
  <pre><code>import "reflect-metadata"
  import { Container } from "inversify"
  import TYPES from './types';
  
  import { ModuleRepo } from '../repo/interfaces'
  import { ModuleARepoImpl } from '../repo/moduleA'
  import { ModuleBRepoImpl } from '../repo/moduleB'
  
  import { ModuleService } from '../services/interfaces'
  import { ModuleAServiceImpl } from '../services/moduleA'
  import { ModuleBServiceImpl } from '../services/moduleB'
  
  const container = Container();
  
  container.bind&lt;ModuleRepo&gt;(TYPES.ModuleARepo).to(ModuleARepoImpl);
  container.bind&lt;ModuleRepo&gt;(TYPES.ModuleBRepo).to(ModuleBRepoImpl);
  
  container.bind&lt;ModuleService&gt;(TYPES.ModuleAService).to(ModuleAServiceImpl);
  container.bind&lt;ModuleService&gt;(TYPES.ModuleBService).to(ModuleBServiceImpl);
  
  export default container;
  </code></pre>
  
  <p>One big problem in the above setting is when the project gets complex, more modules are added resulting in a very long config file (imagine you have dozens of modules). My plan is to divide it into smaller config files, with <code>inversify.config.ts</code> remains the main file.</p>
  
  <p><strong>consider the following settings:</strong></p>
  
  <p><em>./dependencies/interface/index.ts</em></p>
  
  <pre><code>import { Container } from 'inversify';
  export type InversifyContainer = Container
  
  export interface BasicInterface {
          register(container: InversifyContainer): void
          readonly types: Object
      }
  </code></pre>
  
  <p><em>./dependencies/moduleA/index.ts</em></p>
  
  <pre><code>import {InversifyContainer, BasicDependencies} from '../interface';
  import { ModuleRepo } from '../../repo/interfaces'
  import { ModuleARepoImpl } from '../../repo/moduleA'
  import { ModuleService } from '../../services/interfaces'
  import { ModuleAServiceImpl } from '../../services/moduleA'
  
  export class ModuleADependencies {
      register(container: InversifyContainer) {
             container.bind&lt;ModuleRepo&gt;(TYPES.ModuleARepo).to(ModuleARepoImpl);
             container.bind&lt;ModuleService&gt;(TYPES.ModuleAService).to(ModuleAServiceImpl);
      }
  
      readonly types = {
            ModuleARepo: Symbol('ModuleARepo'),
            ModuleAService: Symbol('ModuleAService'),
      }
  }
  </code></pre>
  
  <p><em>./dependencies/inversify.config.ts</em></p>
  
  <pre><code>import "reflect-metadata"
  import { Container } from "inversify"
  
  import { ModuleADependencies } from './moduleA';
  import { ModuleBDependencies } from './moduleB'; // consider moduleB also has the same file
  
  const container = Container();
  const registrationList = [ModuleADependencies, ModuleBDependencies];
  for (const reg of registrationList) {
      new reg().register(container);
  }
  
  export default container;
  </code></pre>
  
  <p><em>./dependencies/types.ts</em></p>
  
  <pre><code>import { ModuleADependencies } from './moduleA';
  import { ModuleBDependencies } from './moduleB';
  
  const TYPES = {
      ...(new ModuleADependencies().types),
      ...(new ModuleBDependencies().types),
  }
  
  export default TYPES
  </code></pre>
  
  <p>However, this way I always have an error showing something like <code>Cannot read property of ModuleARepo of undefined</code> from the types. I browsed the internet however nobody seems to care about how lengthy and messy <code>inversify.config.ts</code> would be if it is in a complex project.</p>
  
  <p>Hoping someone can help with this :)</p>