Skip to content

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

In progress

Description

@pkozlowski-opensource

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Consider the example injection of the ElementRef with a specified generic type:

  // OK: hostEl is of type ElementRef<HTMLElement>, as expected
  hostElExplicit = inject<ElementRef<HTMLElement>>(ElementRef);

  // KO: hostEl is of type ElementRef<any>, expecting ElementRef<HTMLElement>
  hostElInfered = inject(ElementRef<HTMLElement>);

Ideally inject(ElementRef<HTMLElement>) would have the return type of ElementRef<HTMLElement>.

The fix is likely around the type definition for the ProviderToken.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-p38xn9

Activity

  1. modified the milestone: needsTriage on Jan 12, 2024
  2. pkozlowski-opensource commented on Jan 12, 2024

    @pkozlowski-opensource
    MemberAuthor

    Additional info: this issue was discovered while working on the new queries authoring format and boils down to the coding pattern that inject is using as well but it is not specific to inject: https://stackblitz.com/edit/stackblitz-starters-ss5dms

    The typing issue seems to be linked to both the ProviderToken as well as types that are used to compose it:

    export class App {
      // KO: ProviderToken
      viewChildEl = viewChild(ElementRef<HTMLElement>);
      // OK: Type
      viewChildType = viewChildType(ElementRef<HTMLElement>);
      // KO: AbstractType
      viewChildAbstractType = viewChildAbstractType(ElementRef<HTMLElement>);
      // // OK: InjectionToken
      viewChildInjToken = viewChildInjectionToken(HTML_ELEMENT_REF);
    }

    which means that we need to:

    • fix the definition of the AbstractType
    • look into ProviderToken
  3. added
    P3
    on Jan 12, 2024
  4. modified the milestones: needsTriage, Backlog on Jan 12, 2024
  5. eneajaho commented on Jan 12, 2024

    @eneajaho
    Contributor

    Related #48126 #48408

  6. robertIsaac commented on Mar 3, 2024

    @robertIsaac
    Contributor

    this is a blocker for migrating to viewChild signals when combined with { read: ElementRef } since you can not specify the type (and I'm not sure actually why)

    now doing
    matCardEl = viewChild.required(MatCard, { read: ElementRef });
    have error ESLint: Unsafe call of an `any` typed value.(@typescript-eslint/no-unsafe-call)

    and doing
    matCardEl = viewChild.required<ElementRef<HTMLElement>>(MatCard, { read: ElementRef });
    have error
    X [ERROR] TS2554: Expected 1 arguments, but got 2. [plugin angular-compiler]

  7. JeanMeche commented on Mar 3, 2024

    @JeanMeche
    Member

    @robertIsaac In your case you can fix it by specifying both generics defined by viewChild.required<LocatorT, ReadT>(locator: ProviderToken<LocatorT>|string, opts: {read: ProviderToken<ReadT>}): Signal<ReadT>; :

    viewChild.required<MatCard, ElementRef<MatCard>>(MatCard, { read: ElementRef });
  8. robertIsaac commented on Mar 3, 2024

    @robertIsaac
    Contributor

    @robertIsaac In your case you can fix it by specifying both generics defined by viewChild.required<LocatorT, ReadT>(locator: ProviderToken<LocatorT>|string, opts: {read: ProviderToken<ReadT>}): Signal<ReadT>; :

    viewChild.required<MatCard, ElementRef<MatCard>>(MatCard, { read: ElementRef });

    actually I was navigating through the Angular code trying to understand why and then I found it, was here to write that down but found you quickly answering

  9. LayZeeDK commented on Aug 15, 2024

    @LayZeeDK
    Contributor

    Replacing the first signature overload of inject

    export function inject<T>(token: ProviderToken<T>): T;

    with the followng works for inject(ElementRef<HTMLElement>) but does not use ProviderToken<T>.

    export function inject<T>(
      token: T extends Type<infer R>
        ? T
        : T extends AbstractType<infer R>
          ? T
          : T extends InjectionToken<infer R>
            ? T
            : never,
    ): T extends Type<infer R>
      ? R
      : T extends AbstractType<infer R>
        ? R
        : T extends InjectionToken<infer R>
          ? R
          : never;

    This is a noisy solution as it would have to be replicated for all inject signature overloads. We need a way to change ProviderToken<T> by using infer but I haven't been successful in achieving this so far.

  10. 32 remaining items

  11. added a commit that references this issue on Nov 3, 2026
    704d1bd
  12. added 9 commits that reference this issue on Nov 11, 2026
    91969de
    a1b5e97
    952853b
    7f67dc7
    4911874
    d20037f
    01cca4e
    72b3fb5
    ada8b0d
  13. added 5 commits that reference this issue on Nov 24, 2026
    81d2de1
    11ef300
    9628aad
    e844a27
    917340b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: corebugcore: dicross-cutting: types

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions