tsyringe icon indicating copy to clipboard operation
tsyringe copied to clipboard

Inject empty array with @injectAll() in case of unregistered string or symbol token

Open paztek opened this issue 6 years ago • 4 comments

Closes: #63

This PR introduces the following breaking change:

When using @injectAll('myToken') private foo: Foo[] and when nothing is registered for the specified token, it will inject an empty array instead of throwing an "Attempted to resolve unregistered dependency token: ${token.toString()}" error.

This is a breaking change because things that used to break (throwing an error) won't break anymore.

Also, it keeps the same behavior as before when the token is a class constructor and an array of a single instance of the class is injected.

paztek avatar Nov 16 '19 12:11 paztek

CLA assistant check
All CLA requirements met.

msftclas avatar Nov 16 '19 12:11 msftclas

Any ways I can help and unblock this PR? @Xapphire13 I know it's been a while, do you know if you remember the changes you were waiting on?

etiennenoel avatar Jun 16 '21 00:06 etiennenoel

I think the change required was to make it so that the new behavior is not breaking. The idea of an @optional() decorator or being able to pass in a default value to @injectAll() could work. Thinking about it right now, I think the simplest solution would be to do something like @injectAll({defaultValue:[]}). What do you think @Xapphire13 ?

MeltingMosaic avatar Jun 16 '21 20:06 MeltingMosaic

I am just looking today for the same feature. I think the @optional() decorator would be great.

stemyke avatar Jun 28 '21 15:06 stemyke