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.

Injection Not Working With Interfaces

See original GitHub issue

Hi!

What am I doing wrong here?

interface LoggingProvider { 
    error();
}

@Service()
class DefaultLoggingProvider implements LoggingProvider {
    error() {
        // .. logging goes here
    }
}

@Service()
class UsersService {
    constructor(private logger: LogginProvider) { }
}

log is undefined in UsersService unless I name it. Can TypeDI do interface injection without named services or am I missing something here?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
Skona27commented, May 31, 2019

But why? What is the purpose of this, that we must provide specific class in the constructor?

constructor(private logger: LogginProvider)

Isn’t it in conflict with whole idea behind DI pattern?

2reactions
NoNameProvidedcommented, Jul 23, 2018

Hey @ldiego08!

As @Stradivario wrote, interfaces are compiled away and not part of the transformed code. You need to use classes. So you need to import the DefaultLoggingProvider

@Service()
class UsersService {
    constructor(private logger: DefaultLoggingProvider) { }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection not working When Interface is given as ...
Dependency Injection works on Constructor, Field and Interface level. Not at a method parameter level. Hope its clear.
Read more >
Inject functions, not interfaces | Hackle's blog
Inject functions, not interfaces. We'll see how functions can replace interfaces in the case of dependency injection, leading to less and ...
Read more >
DI: Injecting interfaces vs actual classes
One thing to understand, an interface is different than an interface (the keyword).
Read more >
4 solutions for selective injection when the interface has ...
4 solutions for selective injection when the interface has multiple implementations · 1. Description of the problem · 2. Relatively low-level ...
Read more >
How to register dependency injection for set of interfaces as a ...
Autofac has no problem with this, and why it recommends that, because it is going to generate a dynamic proxy anyway. Honestly you...
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