container-ioc icon indicating copy to clipboard operation
container-ioc copied to clipboard

[bug] can't inject values that evaluate to false

Open andrei-tatar opened this issue 7 years ago • 2 comments

Hello,

If I want to register a value of 0/null/false/empty string, it won't get resolved and it will fail.

Ex: container.register({ token: 'foo', useValue: 0 }); container.resolve('foo') fails

The problem seems to be in container.ts : registerOne(provider).

if (provider.useValue) {
  registryData.instance = provider.useValue;
}

I would suggest replacing it with

if (provider.useValue !== void 0) {
  registryData.instance = provider.useValue;
}

or

if ('useValue' in provider) {
  registryData.instance = provider.useValue;
}

to support undefined values.

Also, maybe check if a provider is valid (has only one of useClass/useValue/useFactory set on something different than undefined).

Thanks

andrei-tatar avatar Oct 31 '18 12:10 andrei-tatar

Hey! Thanks for submitting your bug report! Never thought such use case would come up. I'm gonna fix that today.

basedalexander avatar Nov 03 '18 08:11 basedalexander

Glad to be helpful :)

andrei-tatar avatar Nov 05 '18 13:11 andrei-tatar