question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

15reactions
sergidtcommented, Aug 5, 2020

@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!!

5reactions
JoostKcommented, Jul 3, 2020

Thank you for the excellent bug report, this is indeed an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

abstract class constructor throws error - angular - Stack Overflow
Your abstract class has a constructor, so...you have to declare the constructor too in the concrete class, and invoke to super:
Read more >
Fixing the “can't resolve all parameters” exception with ...
Here's what the error looked like: Can't resolve all parameters for QuillConfigurationDirective: [object Object], (?). The (?) indicates that Angular couldn't ...
Read more >
MapStruct 1.5.3.Final Reference Guide
An error will be raised when such an ambiguity is not resolved. For properties which only exist once in the given source objects...
Read more >
JUnit 5 User Guide
Test classes must not be abstract and must have a single constructor. Test Method. any instance method that is directly annotated or meta- ......
Read more >
Groovy Language Documentation
Classes. Normal class; Inner class. Anonymous inner class; Abstract class ... Type checking assignments; List and map constructors; Method resolution ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found