-
Notifications
You must be signed in to change notification settings - Fork 27.1k
inject function does not correctly infer the return type when used with generic classes/abstract classes #48126
Copy link
Copy link
Open
Labels
P4A relatively minor issue that is not relevant to core functionsA relatively minor issue that is not relevant to core functionsarea: coreIssues related to the framework runtimeIssues related to the framework runtimebugcore: dicross-cutting: types
Milestone
Description
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);Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P4A relatively minor issue that is not relevant to core functionsA relatively minor issue that is not relevant to core functionsarea: coreIssues related to the framework runtimeIssues related to the framework runtimebugcore: dicross-cutting: types