fix(core): correct generic inference for inject() and query read with instantiation expressions#68622
Draft
arturovt wants to merge 1 commit intoangular:mainfrom
Draft
fix(core): correct generic inference for inject() and query read with instantiation expressions#68622arturovt wants to merge 1 commit intoangular:mainfrom
arturovt wants to merge 1 commit intoangular:mainfrom
Conversation
… instantiation expressions
`inject(ElementRef<HTMLElement>)` incorrectly returned `ElementRef<any>`, and the same happened for `viewChild('ref', {read: ElementRef<HTMLElement>})`.
This happened because `ProviderToken<T>` includes `AbstractType<T>`. During type inference, TypeScript picked up the default generic type from the `prototype` property (`ElementRef<any>`) instead of preserving the specialized type (`ElementRef<HTMLElement>`).
Fix this by replacing `ProviderToken<T>` with:
* `abstract new (...args: any[]) => T` for class-based tokens
* `InjectionToken<T>` for injection tokens
Apply the same change to the `read` option used by `viewChild`, `viewChildren`, `contentChild`, and `contentChildren` through a private `ClassOrInjectionToken<T>` alias.
Fixes angular#53894
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
inject(ElementRef<HTMLElement>)incorrectly returnedElementRef<any>, and the same happened forviewChild('ref', {read: ElementRef<HTMLElement>}).This happened because
ProviderToken<T>includesAbstractType<T>. During type inference, TypeScript picked up the default generic type from theprototypeproperty (ElementRef<any>) instead of preserving the specialized type (ElementRef<HTMLElement>).Fix this by replacing
ProviderToken<T>with:abstract new (...args: any[]) => Tfor class-based tokensInjectionToken<T>for injection tokensApply the same change to the
readoption used byviewChild,viewChildren,contentChild, andcontentChildrenthrough a privateClassOrInjectionToken<T>alias.Fixes #53894