Skip to content

inject function does not correctly infer the return type when used with generic classes/abstract classes #48126

In progress

Description

@markostanimirovic

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

core

Is this a regression?

No

Description

If we use the inject function with a generic class (or abstract class), the return type won't be properly inferred:

import { inject } from '@angular/core';

class C<T extends number> {}

const c1 = inject(C); // type: C<any>
const c2 = inject(C<1>) // type: C<any>

abstract class AC<T extends string> {}

const ac1 = inject(AC); // type: AC<any>
const ac2 = inject(AC<'ng'>); // type: AC<any>

Typing issue is due to the AbstractType interface. It's currently defined as follows:

interface AbstractType<T> extends Function {
  prototype: T;
}

To solve this typing bug, AbstractType can be defined as follows:

type AbstractType<T> = abstract new (...args: unknown[]) => T;

Here is the Stackblitz playground containing the typedInject function that solves this issue.


This fix would introduce a breaking change.


If accepted, I'll create a PR for this bug.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-qldklj?file=src%2Fapp%2Finject-typing-demo.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 15.0.0

Anything else?

As a workaround, we can explicitly set the return type:

class C<T extends number> {}

const c = inject<C<number>>(C);

Activity

  1. changed the title [-]`inject` function doesn't infer the return type properly when used with generic classes/abstract classes [/-] [+]`inject` function does not correctly infer the return type when used with generic classes/abstract classes[/+] on Nov 19, 2022
  2. added
    area: core
    on Nov 21, 2022
  3. added this to the needsTriage milestone on Nov 21, 2022
  4. pkozlowski-opensource commented on Dec 7, 2022

    @pkozlowski-opensource
    Member

    @markostanimirovic this sounds reasonable. If you could open a PR with a fix it would be fantastic!

  5. added
    P4
    on Dec 7, 2022
  6. modified the milestones: needsTriage, Backlog on Dec 7, 2022
  7. added a commit that references this issue on May 21, 2025
    2400b84
  8. added a commit that references this issue on Nov 2, 2026
    3c510f5
  9. 24 remaining items

  10. added a commit that references this issue on Nov 3, 2026
    704d1bd
  11. added 9 commits that reference this issue on Nov 11, 2026
    91969de
    a1b5e97
    952853b
    7f67dc7
    4911874
    d20037f
    01cca4e
    72b3fb5
    ada8b0d
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

    P4area: corebugcore: dicross-cutting: types

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions