Compiling @Injectable abstract class fails when constructor params cannot be resolved
See original GitHub issue🐞 bug report
Affected Package
This is caused by package @angular/compiler
Is this a regression?
Kind-of - abstract classes didn’t require @Injectable
or @Directive
annotations until v10
Description
Adding @Injectable
annotation to an abstract class, with "strictInjectionParameters": true
, fails compilation if
the compiler can’t resolve constructor parameter injection tokens for the abstract class constructor. This should not fail,
because the abstract class constructor isn’t called by the angular injector at runtime.
🔬 Minimal Reproduction
I’ve attempted to reproduce it in stackblitz, but it seems that stackblitz doesn’t respect the tsconfig "strictInjectionParameters": true
. The code is here:
https://stackblitz.com/edit/angular-ivy-vwamit
Here’s a minimal repro on an ng new --strict --minimal
:
https://github.com/johncrim/ng10-abstract-injectable/commit/a10be0eca1f4225a91946f65acdc46130a2da2b5
🔥 Exception or Error
ERROR in src/app/abstract.service.ts:6:24 - error NG2003: No suitable injection token for parameter 'name' of class 'AbstractService'.
Found string
6 constructor(readonly name: string) { }
~~~~
🌍 Your Environment
Angular Version:
Angular CLI: 10.0.1
Node: 14.4.0
OS: win32 x64
Angular: 10.0.2
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.1000.1
@angular-devkit/build-angular 0.1000.1
@angular-devkit/build-optimizer 0.1000.1
@angular-devkit/build-webpack 0.1000.1
@angular-devkit/core 10.0.1
@angular-devkit/schematics 10.0.1
@angular/cli 10.0.1
@ngtools/webpack 10.0.1
@schematics/angular 10.0.1
@schematics/update 0.1000.1
rxjs 6.5.5
typescript 3.9.6
webpack 4.43.0
Anything else relevant? See: https://github.com/angular/angular/pull/32987
It appears that a fix for this issue was added for the @Directive
decorator (allowing abstract constructors to not be injectable), but not for @Injectable
.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:9 (5 by maintainers)
Top GitHub Comments
@pheinicke Providing a provider for any abstract class could be a solution, but is not the best solution. Any developer should be able to extend any abstract class passing parameters, is a OOP rule, without resolving via Dependency Injector.
In my product with thousands of files, I cannot resolve this kind of error declaring providers. In my opinion is a bug with high priority!!
Thank you for the excellent bug report, this is indeed an issue.