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

This issue has been created since 2022-11-19.

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

core

Is this a regression?

No

Description

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

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);
More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-12-01
Star Count 85220
Watcher Count 3042
Fork Count 22607
Issue Count 1316

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
3: couldn't match all kex parts 23 2015-10-10 2022-11-20
Open and edit a component 0 2020-11-26 2022-10-31
Create some common components 0 2020-11-26 2022-10-20
Emulate the Kria alt firmware for White Whale 1 2017-12-13 2022-11-15
Instability when Simulating Suture Thread 1 2021-11-09 2022-10-30
KeycloakService throws error when navigating back from login page 0 2021-10-18 2022-11-05
[Feature Request] Add support for games being launched by frontends 1 2021-04-30 2022-11-02
Convert CircleCI cron jobs to scheduled pipelines 0 2022-03-07 2022-11-15
Example workflow mentions variants.sh, but it could not be found 3 2021-10-12 2022-10-14
Firefox docker has issues 2 2022-03-04 2022-10-25
All fonts in the application are Chinese except in ttu 15 2022-09-18 2022-11-21
[Bug]: Activating extension 'vscjava.vscode-java-test' failed: Cannot read properties of undefined (reading 'name'). 10 2022-05-13 2022-11-30
NEthereum extension support 2 2021-11-12 2022-11-13
Slack invite link broken 2 2021-03-11 2022-11-19
Modal stacks when adding new enroll secret 2 2022-11-09 2022-11-14
Missing timestamp in RESP conversion's "start date" 6 2021-09-09 2022-11-23
PubMed as data source 2 2022-01-05 2022-11-29
Should change sharp version for M1 macs 1 2022-05-12 2022-11-17
about metrics calc by inception network ? 11 2021-10-14 2022-11-16
[BUG] No error thrown for invalid condition 16 2021-12-21 2022-10-01
Two keystokes registered on single button press 0 2022-05-12 2022-09-25
anyOf 的相关升级优化 3 2021-07-03 2022-01-07
Code line escapes from a code block 0 2022-11-25 2022-11-24
Dockerイメージ更新 0 2020-08-10 2021-12-31
ステータスページを用意する 0 2020-06-18 2021-12-31
Dockerイメージのタグ運用をする 0 2020-04-11 2021-12-31
DBにuser_nameも持たせたい 0 2020-03-29 2021-12-31
MongoDB用ライブラリの変更 0 2020-08-16 2021-12-31
versionを画面から出力したい 0 2020-03-29 2021-12-31
DockerAPI化 1 2020-08-10 2021-12-31
[Structural] allow structural solver to use a submodelpart as computing model part 15 2021-09-15 2022-09-15
Declare browser support 0 2016-11-18 2022-11-23
Media manager performance investigation 0 2022-05-15 2022-10-29
Allow Add or Remove contentlet to a specific page's version variant 1 2022-10-12 2022-10-25
Add Prometheus stats throughout the app 0 2022-11-03 2022-11-21
`json` module has no `decode` member in bzlmod module implementation 4 2022-01-10 2022-07-21
[Bug Report] Generating forget the first `scene_preview` 1 2021-10-23 2022-10-27
Error view & delete image 1 2022-07-15 2022-08-06
[Feature Request] Allow daq.GraduatedBar() to auto-scale with the container 0 2022-03-16 2022-11-23
[Adaptive Card Templating] Problem with double-quotes in data 1 2022-11-23 2022-11-22
Many tests fail with Mesa 9 2019-10-05 2022-10-20
Integration with Linkerd 6 2022-08-27 2022-11-17
Can't use with stackblitz react-ts, Cannot use import statement outside a module 4 2022-04-27 2022-11-27
Bump url-parse from 1.4.7 to 1.5.1 in /themes/custom/apigee_kickstart 1 2021-05-07 2021-11-07
Revs_diff returns a long list of deleted possible_ancestors 7 2022-05-23 2022-11-27
Introduce mutating operator. 0 2021-01-18 2022-01-25
报错:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256) 1 2022-09-29 2022-11-06
Missing validation (timezone) for EmbargoDate attribute 0 2022-05-30 2022-06-23
MongoNotConnectedError on test using mongodb-memory-server and jest 9 2022-03-28 2022-09-11
remove xalan dependency due to it being end of life 16 2022-07-19 2022-11-22