# Dependency Injection in Typescript with tsyringe

DevFeed: [Dependency Injection in Typescript with tsyringe](<https://devfeed.tech/articles/dependency-injection-in-typescript-with-tsyringe-19830.md>)

Original publisher: [Read original article](<https://tech.gc.com/dependency-injection/>)

Author: GameChanger

Published: 2020-11-30T23:47:04Z

Content type: article

Language: en

Sources: [GameChanger](<https://devfeed.tech/sources/gamechanger.md>)

Topics: [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [TypeScript](<https://devfeed.tech/topics/typescript.md>), [Framework](<https://devfeed.tech/topics/framework.md>)

Tags: [dependencies](<https://devfeed.tech/tags/dependencies.md>), [dependency](<https://devfeed.tech/tags/dependency.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [framework](<https://devfeed.tech/tags/framework.md>), [tests](<https://devfeed.tech/tags/tests.md>), [typescript](<https://devfeed.tech/tags/typescript.md>)

## AI overview

This article explains dependency injection in TypeScript, including manual constructor-based injection and the use of mocks for testing. It then introduces Tsyringe as a dependency injection framework that provides injectable decorators and a dependency container for resolving classes.

## Source excerpt

Why Dependency Injection In any large object oriented codebase, managing dependencies can get difficult. Each class can require any number of third parties or other classes to function, and it can be hard to test the behavior of a single class with mocks if those dependencies aren't easy to provide. Fortunately, there's a popular design pattern that can be applied to solve this problem, and that is dependency injection. When using dependency injection, classes can be provided their dependencies through a constructor, and those dependencies can be swapped out easily for other implmentations. In tests, mocks can simply be substituted in to test class behavior. While most of the time, this pattern is implemented with a framework, even without one manual dependency injection can give you some of these benefits. Here at Gamechanger, we previously had a form of manual dependency injection in our typescript codebase. Each class would have a constructor that accepted its dependencies, which could be swapped out with mocks or a real implementation. class BusinessLogic { constructor (dependencyA: DependencyA, dependencyB: DependencyB) {} private void foo() { this.dependencyA.action(); } } // To instantiate this class, both dependencies must be created const businessLogic = new BusinessLogic(new DependencyA(), new DependencyB()); // to test this class, mocks can be passed in const testBusinessLogic = new BusinessLogic(new MockDependencyA(), new MockDependencyB()); In this example, if you need to replace a dependency, you can just supply a different class in the constructor for BusinessLogic. This can work great for a small number of classes with a tiny dependency tree, but as your codebase's number of dependencies grow, it can become difficult to manage. Once your dependencies have dependencies, its not as straightforward to get an instance of a class. class DependencyC { constructor(dependencyE, DepedencyE) { } } class DependencyA { constructor(dependencyC: DependencyC, depen