Questions tagged [tsyringe]
40 questions
14
votes
2 answers
Jest tests - Please add 'import "reflect-metadata"' to the top of your entry point
I'm developing an application using dependency injection with tsyringe. That's the example of a service that receives the repository as a dependency:
import { injectable, inject } from 'tsyringe'
import IAuthorsRepository from…

Laura Beatris
- 1,782
- 7
- 29
- 49
5
votes
1 answer
Dynamic Dependency injection with Typescript using tsyringe
I am trying to build and example to understand how the DI framework/library works, but i am encountering some problems.
I have this interface with two possible implementations:
export interface Operation {
calculate(a: number, b: number):…

JMir
- 137
- 1
- 10
4
votes
1 answer
Dependancy injection using Tsyringe for multiple implementation of interface in Typescript
Context
I am currently working on a Typescript Lambda project where we are planning to refactor our code to make use of dependency injection using the Tsyringe library. We have a typical MVC structure for projects except instead of the Repo/Database…

sebin vincent
- 330
- 3
- 12
3
votes
1 answer
Cannot inject the depency at position #0 because TypeInfo not known
I'm having this trouble:
[ERROR] 14:07:18 Error: Cannot inject the dependency at position #0 of "ListInterviewPagesUseCase" constructor. Reason: TypeInfo not known for "InterviewPagesRepository"
Container
import { container } from…

Leonardo Theodoro
- 73
- 1
- 9
3
votes
4 answers
React with TypeScript using tsyringe for dependency injection
I am currently having trouble with my React TypeScript project.
I created my project with npx create-react-app my-app --template typescript.
I recently added tsyringe for dependency injection and was trying to implement it for an apiService. After…

Jordan Schnur
- 1,225
- 3
- 15
- 30
2
votes
1 answer
tsyringe container.resolve creates a new instance on call in custom decorator
I'm trying to create a custom decorator for tsyringe to inject through properties.
My code:
import '@abraham/reflection';
import {container} from 'tsyringe';
/**
* Inject class through property
*
* @param targetClass
* @returns…

NECMI
- 71
- 5
1
vote
2 answers
How to register different implementations of an interface in a tsyringe container based on a given parameter
I am trying to find an elegant way to inject an Interface into the tsyringe container based on a given value. (string/enum/Symbol)
This is how I would do it with a switch..case clause:
interface IColor{
someMethod():void;
readonly…

zlZimon
- 2,334
- 4
- 21
- 51
1
vote
0 answers
Tsyringe's `resolve` function throws "TypeInfo not known for X" error when using register methods with dependencies
I was playing with Tsyringe. Seems like it works well when I use decorators however I couldn't make it work with register function.
Here are what I've tried
Test 1: OK
Here I simply use @singleton() decorator, it works fine!
@singleton()
class…

Mehmet Ataş
- 11,081
- 6
- 51
- 78
1
vote
0 answers
typescript error on tsyringe delay function
I'm trying to inject some dependency with tsyringe delay function but the typescript are warning me with the follow error:
Type 'string' is not assignable to type 'constructor'.ts(2322)
lazy-helpers.d.ts(9, 54): The expected type comes from the…

Kauan Polydoro
- 38
- 5
1
vote
0 answers
Why do TSyringe and reflect-metadata need to be installed as dependencies and not dev dependencies?
I recently installed TSyringe into a Node project using TypeScript. TSyringe also needs reflect-metadata.
I initially installed these packages as dev dependencies and things worked fine in my dev env/TS land. But when I ran a production build I got…

eysikal
- 579
- 1
- 6
- 13
1
vote
1 answer
Is it possible to inject a mix of runtime value and normal dependencies using tsyringe?
I'm using @launchtray/tsyringe (a fork of Microsoft tsyringe with async initialization support) which is quite handy.
I'd like to implement something like the following:
A orchestration service whose constructor accepts dependency services (can be…

Eric Xin Zhang
- 1,230
- 15
- 20
1
vote
1 answer
What does the @inject() do in dependency injection with tsyringe?
I'm learning DI with tsyringe. I'm also totally new to the concept of DI.
The following snippet works and the container.resolve(Foo) properly instantiate the dependencies.
import "reflect-metadata";
import { injectable, container } from…

Wajahath
- 2,827
- 2
- 28
- 37
1
vote
0 answers
Where's The Circular Dependency?
I'm writing a CLI using TSyringe and getting an error. I'm not exactly sure I know why.
Error: Attempted to construct an undefined constructor. Could mean a circular dependency problem. Try using `delay` function.
Here's the code:
//---…

alphadogg
- 12,762
- 9
- 54
- 88
1
vote
1 answer
TSyringe: How to change a parameter of an injected object?
I have an app with multiple different components, each component having their own dependency and am using TSyringe to manage dependency injection. One of the components is an operator such as this:
// myoperator.ts
@injectable()
export class…

hyk
- 75
- 6
1
vote
0 answers
Use socket.io in controllers through dependency injection
Stack: Node.js + Express + TypeScript + Socket.io + tsyringe
Architecture: There's a WebServer class which creates the socket connection and then passes the io and the socket objects to the SocketController class .
Both classes are decorated with…

Omid
- 301
- 1
- 2
- 12