-
Notifications
You must be signed in to change notification settings - Fork 169
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
Inject an empty array with @injectAll() when nothing registered for the token #63
Comments
Hum OK. I see. That would introduce different behaviors for Is there a workaround for my use case ? |
Here is the PR if you're interested: #64 |
I spoke briefly with @MeltingMosaic and we agreed that the default behaviour for non-registered resolutions should be to fail. However, I can see a case where we would want to expose opt-in functionality to not fail if, and only if the target is an array or a nullable type. My best guess of how to opt-in would be a flag in the resolution options. How does that sound? |
Hi @Xapphire13, If I understand correctly, you suggest something like this: I think the Angular project solves this issue (cf APP_INITIALIZER, HTTP_INTERCEPTOR, etc.) with an additional I’d be OK with both solutions. |
I was originally thinking of the first example (maybe with a name like Do you have time to code up a PR? |
@Xapphire13 I can try.
Intuitively, 1. seems easier to me. |
@paztek, could you give option 2 a stab? I assume the decorator would be on the parameters being injected right? |
I like the idea of an |
I think it would be safe to allow |
Maybe |
Have there been any updates on this issue? The Edit: I created my own PR which tries to implement this if it's any use to anyone. |
Totally agree with @SirBernardPhilip |
I ran into the same issue, @SirBernardPhilip's solution would be optimal for my use case. Any plans on merging it? |
Is your feature request related to a problem? Please describe.
I'm using
@injectAll('feature')
to inject an array ofFeature
s into aServer
. I'd like to unit test some of the mechanisms of theServer
class but the resolve fails when zero feature has been registered on the container.Description
Would it make more sense to inject an empty array with
@injectAll()
when no service has been registered on the container and the token is unknown by the container?Alternate solutions
I tried
@injectAll('feature') private readonly features?: Feature[]
or@injectAll('feature') private readonly features: Feature[] = []
but same error.Additional context
By looking (quickly) at the code, it seems to be a one-line change at https://github.com/microsoft/tsyringe/blob/master/src/dependency-container.ts#L242 (apart from the tests) but maybe it breaks things or maybe it's not how it should behave.
I can try submitting a PR if that's OK with you.
The text was updated successfully, but these errors were encountered: