-
Notifications
You must be signed in to change notification settings - Fork 25.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tree Shakable Providers and InjectionToken: inject() does not work with abstract classes #23611
Comments
Relates to microsoft/TypeScript#17572, there's no way to type-check an abstract class. |
Yes, but we could go with any. I know, this is not a pretty solution but it fits to Angular b/c a token can technically be of any type (also see https://angular.io/api/core/Inject). |
Not actually, |
I see. That means, we have two options.
None of them is perfect. But when we consider that it is very common to use abstract classes as tokens, I think option 2 is the better one. Also, there are many cases where Angular follows this pattern and it was possible in the past, which means that option 1 would break things we are used to. |
Not since v4, it already won't work in current angular/packages/core/src/di/injector.ts Lines 74 to 79 in 8062f7d
Whatever deprecated in v4 should be removed in v6. So this request is NOT about the new |
I see. Thanks for pointing this out. Frankly, this is not ideal, since using abstract base types for such things is in general considert good design. |
Trying to resolve my difficulties with OnPush change detection in unit tests and I needed to get the ChangeDetectorRef of my Component to trigger change detection. But that caused these warnings because ChangeDetectorRef is an abstract class. |
We just enabled the "deprecation"-rule of tslint and I noticed a few of them in our app. |
Support for abstract classes as tokens was added in #37958, so closing this as resolved. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
You cannot use the new inject with abstract classes, as it expects an InjectionToken or a Type which is defined as "new-able":
Expected behavior
It should be possible to use inject with abstract classes.
Sidenote: inject from @angular/core/testing uses any instead and this includes abstract classes too.
Minimal reproduction of the problem with instructions
Please see comments in
https://stackblitz.com/edit/angular-yxr5ny-foth3r?file=src%2Fapp%2Fapp.tokens.ts
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: