title: Dynamic Dependency injection with Typescript using tsyringe
body: <p>I am trying to build and example to understand how the DI framework/library works, but i am encountering some problems.</p>
  <p>I have this interface with two possible implementations:</p>
  <pre><code>export interface Operation {
      calculate(a: number, b: number): number;
  }
  </code></pre>
  <p>sub.ts</p>
  <pre><code>import { Operation } from &quot;./operation.interface&quot;;
  
  export class Sub implements Operation {
      calculate(a: number, b: number): number {
          return Math.abs(a - b);
      }
  }
  </code></pre>
  <p>sum.ts</p>
  <pre><code>import { Operation } from &quot;./operation.interface&quot;;
  
  export class Sum implements Operation {
      calculate(a: number, b: number): number {
          return a + b;
      }
  
  }
  </code></pre>
  <p>calculator.ts</p>
  <pre><code>import { Operation } from &quot;./operation.interface&quot;;
  import {injectable, inject} from &quot;tsyringe&quot;;
  
  @injectable()
  export class Calculator {
      constructor(@inject(&quot;Operation&quot;) private operation?: Operation){}
  
      operate(a: number, b: number): number {
          return this.operation.calculate(a, b);
      }
  }
  </code></pre>
  <p>index.ts</p>
  <pre><code>import &quot;reflect-metadata&quot;;
  import { container } from &quot;tsyringe&quot;;
  import { Calculator } from &quot;./classes/calculator&quot;;
  import { Sub } from &quot;./classes/sub&quot;;
  import { Sum } from &quot;./classes/sum&quot;;
  
  container.register(&quot;Operation&quot;, {
      useClass: Sum
  });
  
  container.register(&quot;OperationSub&quot;, {
      useClass: Sub
  });
  
  const calculatorSum = container.resolve(Calculator);
  
  const result = calculatorSum.operate(4,6);
  console.log(result);
  
  // const calculatorSub = ???
  </code></pre>
  <p>is there a way where I could have two calculators with different behaviours or am I doing it completely wrong?</p>