fix(core): infer correct return type when inject is used with generic classes#48408
fix(core): infer correct return type when inject is used with generic classes#48408markostanimirovic wants to merge 1 commit intoangular:mainfrom
inject is used with generic classes#48408Conversation
4329648 to
c94506e
Compare
c94506e to
efbdaba
Compare
|
Some of the tests that use If we pass a class without constructor arguments, the right overload signature will be matched. On the other hand, when we pass a class with constructor arguments, the expected signature will be skipped and the last one (with I reported this behavior here: microsoft/TypeScript#51899 type Type<T> = new (...args: any[]) => T;
function inject<T>(t: Type<T>): T;
function inject(t: any): any; // comment out this line => the type of `c2` will be `ClassWithCtorArgs`
function inject(t: any): any {
return t;
}
class Class {}
class ClassWithCtorArgs {
constructor(x: number) {}
}
// type: Class ✅
const c1 = inject(Class);
// type: any ❌
const c2 = inject(ClassWithCtorArgs);Before this PR, the right signature was matched because of the |
|
Given that |
Yes, for sure. Another solution could be to remove the overload signature with |
|
Hi @pkozlowski-opensource, @atscott 👋 Have you considered removing deprecated signatures with |
|
@markostanimirovic We will not have time to work on a migration for v16. |
|
Hi everyone! |
|
I stumbled upon this today. Hopefully this PR gets merged soon! |
|
Per discussions in this PR, the fix here is a regression in the more common cases. The ideal path forward is to remove the overload with |
|
I think I misunderstood the comment history here. The correct fix requires this change and the removal of the deprecated |
Yes 👍 |
|
Would it be possible to use |
|
Another possible option is to add the current definition of type Type<T> = new (...args: any[]) => T;
function inject<T>(t: Type<T>): T;
function inject<T>(t: Function & {prototype: T}): T // <- new
function inject(t: any): any;
function inject(t: any): any {
return t;
}
class ClassWithCtorArgs {
constructor(x: number) {}
}
// infers ClassWithCtorArgs ✅
const c2 = inject(ClassWithCtorArgs);The obvious downside is that it adds yet another overload that needs to be removed eventually, and unlike |
|
Any news concerning this? It makes some signatures really long |
This commit further restricts the deprecated type on injector.get to exclude all but `string`. Progresses towards angular#48408 BREAKING CHANGE: The `any` overload has been removed from `injector.get`. It now only supports `ProviderToken<T>` and (deprecated since v4) `string`.
efbdaba to
66940df
Compare
|
#60202 was submitted to remove the |
…ic classes This commit sets the correct type for `AbstractType`. With this change, the return type of the `inject` function will be correctly inferred when a class/abstract class with generics is passed as an input argument. Closes angular#48126
66940df to
2400b84
Compare
|
Unfortunately this is way too breaking, components with |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #48126
What is the new behavior?
The return type of the
injectfunction is correctly inferred when a class/abstract class with generics is passed as an input argument.Does this PR introduce a breaking change?
This commit introduces a breaking change with types when
injectis used with generic classes/abstract classes.BEFORE:
The inferred generic type is always
any:AFTER:
The generic type is properly inferred: