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.

Can not inject config param in constructor

See original GitHub issue

When I try to inject a param in the constructor of my class using the ConfigParam decorator on the function argument and the Configurable decorator in the function, as suggested by the docs, TypeScript is throwing an error on the Configurable decorator.

This is my code:

@Configurable()
public constructor(@ConfigParam('app.env') private readonly env: string) { }

And this is the error: TS1206: Decorators are not valid here.

I managed to make it work with this:

private readonly env: string;

public constructor(private readonly config: ConfigService) {
  this.env = config.get('app.env');
}

but that defeats completly the purpose of the decorators.

Am I doing something wrong?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
bashleighcommented, Sep 7, 2018

I don’t think you can apply a method decorator on the constructor? We could develop a class decorator but this would also defeat the purpose as we’d override the constructor method to inject the ConfigService. Where are you using the configured variable?

It might be possible to use the static method on the ConfigService within your constructor.

export class Test {
  constructor() {
    this.property = ConfigService.get('app.env');
  }
}

Else I would use the Configurable decorator on the class’s methods where you would use the variable. Or another method would be to use a factory

import {Module} from '@nestjs/common';

@Module({
  imports: [ConfigModule],
  providers: [{
    useFactory: (config: ConfigService) => new Test(config.get('app.env')),
    inject: [ConfigService],
})
export class TestModule {}

Not really what you want though is it? But doesn’t inject into your provider.

0reactions
FahmyChaabanecommented, Apr 23, 2022

Hello from the future !!

I’m stuck at this. was there any workaround for this ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can not inject config param in constructor · Issue #30 - GitHub
When I try to inject a param in the constructor of my class using the ConfigParam decorator on the function argument and the...
Read more >
Constructor Injection using Parameter - java - Stack Overflow
The current logic is working fine now. Please suggest if this implementation is not good or will cause issue anywhere. Will it set...
Read more >
Constructor Dependency Injection in Spring - Baeldung
This quick tutorial will explore a specific type of DI technique within Spring called Constructor-Based Dependency Injection, which simply ...
Read more >
Assisted Injection - Dagger
Assisted injection is a dependency injection (DI) pattern that is used to ... This is true even if the constructor does not contain...
Read more >
3.4.1.1 Constructor-based dependency injection - Spring
One possible solution is to edit the source code of some classes to be configured by setters rather than constructors. Alternatively, avoid constructor ......
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