Skip to content

Compiling @Injectable abstract class fails when constructor params cannot be resolved #37914

@johncrim

Description

@johncrim

🐞 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:
johncrim/ng10-abstract-injectable@a10be0e

🔥 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: #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.

Metadata

Metadata

Assignees

Labels

P4A relatively minor issue that is not relevant to core functionsarea: compilerIssues related to `ngc`, Angular's template compilerfreq2: mediumstate: has PRtype: bug/fix

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions