Typescript - Interface injecting
See original GitHub issueHi,
is it possible to inject a component based on its interface?
Let’s assume the following component:
class UserRepository implements IUserRepository
This component is injected into the controller by interface from which is implemented:
export class UserController {
constructor(private userService: IUserRepository) {}
It would be great to be able to set dependencies as follows:
@Module({
controllers: [ UsersController ],
components: [
{ provide: IUserRepository, useClass: UserRepository }
],
})
Thanks
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Top 5 TypeScript dependency injection containers
Dependency injection (DI): a variant of IoC in which objects receive other objects as dependencies instead of constructors or setters ...
Read more >Dependency Injection in TypeScript - DEV Community
The container will know which objects with which interfaces are required by each of the modules. It will also know which objects implement...
Read more >Setting Up Dependency Injection with TypeScript in an Object ...
Dependency injection in classes#. We can inject a dependency in a class using a setter or a constructor. We will latter. A constructor...
Read more >Dependency Injection in TypeScript - nehalist.io
Dependency injection is a technique whereby one object supplies the dependencies of another object. Quote from Wiki. What does that mean?
Read more >Dependency Injection in TypeScript | by Mert Türkmenoğlu
The dependency injection principle tells us that a class should receive its dependencies rather than instantiating them. Delegating object ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Using Interfaces as value types is not possible as they only exist during development. After transpiling interfaces no longer exist resulting in empty object values. There is a solution for your problem though using string keys as provide values and the inject decorator:
Also see here
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.