question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript - Interface injecting

See original GitHub issue

Hi,

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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
JulianBiermanncommented, May 9, 2017

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:

@Module({
    controllers: [ UsersController ],
    components: [
        { provide: 'IUserRepository', useClass: UserRepository }
    ],
})
export class UserController {
    constructor(@Inject('IUserRepository') private userService: IUserRepository) {}

Also see here

0reactions
lock[bot]commented, Sep 25, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found