Skip to content

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

@andrei-tatar

Description

@andrei-tatar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions