Skip to content

fix(core): correct generic inference for inject() and query read with instantiation expressions#68622

Draft
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/issue-53894
Draft

fix(core): correct generic inference for inject() and query read with instantiation expressions#68622
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/issue-53894

Conversation

@arturovt
Copy link
Copy Markdown
Contributor

@arturovt arturovt commented May 7, 2026

inject(ElementRef<HTMLElement>) incorrectly returned ElementRef<any>, and the same happened for viewChild('ref', {read: ElementRef<HTMLElement>}).

This happened because ProviderToken<T> includes AbstractType<T>. During type inference, TypeScript picked up the default generic type from the prototype property (ElementRef<any>) instead of preserving the specialized type (ElementRef<HTMLElement>).

Fix this by replacing ProviderToken<T> with:

  • abstract new (...args: any[]) => T for class-based tokens
  • InjectionToken<T> for injection tokens

Apply the same change to the read option used by viewChild, viewChildren, contentChild, and contentChildren through a private ClassOrInjectionToken<T> alias.

Fixes #53894

@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label May 7, 2026
… instantiation expressions

`inject(ElementRef<HTMLElement>)` incorrectly returned `ElementRef<any>`, and the same happened for `viewChild('ref', {read: ElementRef<HTMLElement>})`.

This happened because `ProviderToken<T>` includes `AbstractType<T>`. During type inference, TypeScript picked up the default generic type from the `prototype` property (`ElementRef<any>`) instead of preserving the specialized type (`ElementRef<HTMLElement>`).

Fix this by replacing `ProviderToken<T>` with:

* `abstract new (...args: any[]) => T` for class-based tokens
* `InjectionToken<T>` for injection tokens

Apply the same change to the `read` option used by `viewChild`, `viewChildren`, `contentChild`, and `contentChildren` through a private `ClassOrInjectionToken<T>` alias.

Fixes angular#53894
@arturovt arturovt force-pushed the fix/issue-53894 branch from 8e97995 to a11e5bd Compare May 7, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inject of ElementRef does not use generic type for the returned type inference

1 participant