Skip to content

useValue and useFactory providers throws error when value.constructor is null #56552

@michael-dean-haynie

Description

@michael-dean-haynie

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

I'm importing an object from a 3rd party library (let's call it tplObj) that that has a null constructor.

tplObj.constructor === null; // > true

For testing purposes I'm using an angular provider to make the object injectable/mockable.

@Component({
  ...
  providers: [{ provide: LIB_OBJ_TOKEN, useValue: tplObj }],
  ...
})
export class App {
  constructor(@Inject(LIB_OBJ_TOKEN) private libObj: any) {}
}

When I run the app (ng serve) angular throws an error in the console (see below) and the webpage never gets populated.
From stepping through the debugger, it seems like it could be a simple oversight requiring a null check:

image

If this is the expected behavior, I'm not sure why. It does seem odd to have a null constructor, but I believe the reason the 3rd party library made it like that was to effect singleton behavior. So perhaps understandable. Seems like something the angular DI could support. My current workaround is just to manually update the constructor to be a function with a singleton-like implementation.

NOTE: I use useValue in the example above, but I use a useFactory approach in the stackblitz example. They both cause the same error.

NOTE: To see the console error in stackblitz you need to open the preview in a new tab.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-j1trbn?file=src%2Fapp%2Fapp%2Fapp.component.ts

Please provide the exception or error you saw

ERROR TypeError: Cannot read properties of null (reading 'ɵcmp')
    at getComponentDef (chunk-MVMP4A3Y.js?v=2eb1452d:5780:14)
    at isStandaloneComponent (chunk-MVMP4A3Y.js?v=2eb1452d:21911:15)
    at handleInstanceCreatedByInjectorEvent (chunk-MVMP4A3Y.js?v=2eb1452d:21897:42)
    at handleInjectorProfilerEvent (chunk-MVMP4A3Y.js?v=2eb1452d:21845:5)
    at chunk-MVMP4A3Y.js?v=2eb1452d:21838:50
    at injectorProfiler (chunk-MVMP4A3Y.js?v=2eb1452d:4840:5)
    at emitInstanceCreatedByInjectorEvent (chunk-MVMP4A3Y.js?v=2eb1452d:4865:3)
    at getNodeInjectable (chunk-MVMP4A3Y.js?v=2eb1452d:7873:20)
    at searchTokensOnInjector (chunk-MVMP4A3Y.js?v=2eb1452d:7824:12)
    at lookupTokenUsingNodeInjector (chunk-MVMP4A3Y.js?v=2eb1452d:7774:26)

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 18.0.5
Node: 18.20.3
Package Manager: npm 10.2.3
OS: linux x64

Angular: 18.0.4
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.5
@angular-devkit/build-angular   18.0.5
@angular-devkit/core            18.0.5
@angular-devkit/schematics      18.0.5
@angular/cli                    18.0.5
@schematics/angular             18.0.5
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.7

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions