Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: Inject Private Property #823

Open
bestickley opened this issue Jul 21, 2022 · 2 comments
Open

question: Inject Private Property #823

bestickley opened this issue Jul 21, 2022 · 2 comments
Labels
type: question Questions about the usage of the library.

Comments

@bestickley
Copy link

I was trying to...
inject a private property:
image

The problem:
I'm getting the TypeScript error: "Decorators are not valid here"

@bestickley bestickley added the type: question Questions about the usage of the library. label Jul 21, 2022
@attilaorosz
Copy link
Member

That is expected, private fields cannot be decorated by design. However, you can try to create a getter and decorate that. (I haven’t tried it myself, but the spec allows it)

@wladzynski
Copy link

wladzynski commented Sep 28, 2023

@attilaorosz - this is not true. It used to work in earlier versions. Started to acting up around 0.8.0. Currently with 0.10.0 I find, that @Inject() works in one place in code and fails in others. Calling container directly via Container.get in the same place works with no issue.

This used to work but doesn't anymore with 0.10.0:

@Service()
@RestController('some-settings', 2)
export class SomeSettingsV2Resource {
    @Inject()
     private getAlHandler: GetAllHandler;

    @Inject()
    private getSomeSettingsHandler: GetSomeSettingsHandler;

}

this also doesn't work:

@Service()
@RestController('some-settings', 2)
export class SomeSettingsV2Resource {
    constructor(
        @Inject(() => GetAllHandler)
        private readonly getAllHandler: GetAllHandler,
        @Inject(() => GetSettingHandler)
        private readonly getSettingHandler: GetSettingHandler
    ) {} 
}

This works though:

@Service()
@RestController('some-settings', 2)
export class SomeSettingsV2Resource {
   
    constructor(
        private readonly getAllHandler: GetAllHandler = Container.get(GetAllHandler),
        private readonly getSettingHandler: GetSettingHandler = Container.get(GetSettingHandler)
    ) {}
       
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

3 participants