You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inject of ElementRef does not use generic type for the returned type inference #53894
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimebugcore: dicross-cutting: types
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
Consider the example injection of the ElementRef with a specified generic type:
// OK: hostEl is of type ElementRef<HTMLElement>, as expectedhostElExplicit=inject<ElementRef<HTMLElement>>(ElementRef);// KO: hostEl is of type ElementRef<any>, expecting ElementRef<HTMLElement>hostElInfered=inject(ElementRef<HTMLElement>);
Ideally inject(ElementRef<HTMLElement>) would have the return type of ElementRef<HTMLElement>.
The fix is likely around the type definition for the ProviderToken.
Please provide a link to a minimal reproduction of the bug
Reacted by Sergej Sintschilin, nifiro, Joe Percy, Enea Jahollari, keatkeat87, ionut-gheorghe, Harpush, Lars Gyrup Brink Nielsen, sandy-fairsupply, Daniel Kimmich and 5 more
this is a blocker for migrating to viewChild signals when combined with { read: ElementRef } since you can not specify the type (and I'm not sure actually why)
now doing matCardEl = viewChild.required(MatCard, { read: ElementRef });
have error ESLint: Unsafe call of an `any` typed value.(@typescript-eslint/no-unsafe-call)
and doing matCardEl = viewChild.required<ElementRef<HTMLElement>>(MatCard, { read: ElementRef });
have error X [ERROR] TS2554: Expected 1 arguments, but got 2. [plugin angular-compiler]
@robertIsaac In your case you can fix it by specifying both generics defined by viewChild.required<LocatorT, ReadT>(locator: ProviderToken<LocatorT>|string, opts: {read: ProviderToken<ReadT>}): Signal<ReadT>; :
@robertIsaac In your case you can fix it by specifying both generics defined by viewChild.required<LocatorT, ReadT>(locator: ProviderToken<LocatorT>|string, opts: {read: ProviderToken<ReadT>}): Signal<ReadT>; :
actually I was navigating through the Angular code trying to understand why and then I found it, was here to write that down but found you quickly answering
with the followng works for inject(ElementRef<HTMLElement>) but does not use ProviderToken<T>.
exportfunctioninject<T>(token: TextendsType<infer R>
? T
: TextendsAbstractType<infer R>
? T
: TextendsInjectionToken<infer R>
? T
: never,): TextendsType<infer R>
? R
: TextendsAbstractType<infer R>
? R
: TextendsInjectionToken<infer R>
? R
: never;
This is a noisy solution as it would have to be replicated for all inject signature overloads. We need a way to change ProviderToken<T> by using infer but I haven't been successful in achieving this so far.
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimebugcore: dicross-cutting: types
Activity
pkozlowski-opensource commentedon Jan 12, 2024
Additional info: this issue was discovered while working on the new queries authoring format and boils down to the coding pattern that
injectis using as well but it is not specific toinject: https://stackblitz.com/edit/stackblitz-starters-ss5dmsThe typing issue seems to be linked to both the
ProviderTokenas well as types that are used to compose it:which means that we need to:
AbstractTypeProviderTokeneneajaho commentedon Jan 12, 2024
Related #48126 #48408
robertIsaac commentedon Mar 3, 2024
this is a blocker for migrating to
viewChildsignals when combined with{ read: ElementRef }since you can not specify the type (and I'm not sure actually why)now doing
matCardEl = viewChild.required(MatCard, { read: ElementRef });have error
ESLint: Unsafe call of an `any` typed value.(@typescript-eslint/no-unsafe-call)and doing
matCardEl = viewChild.required<ElementRef<HTMLElement>>(MatCard, { read: ElementRef });have error
X [ERROR] TS2554: Expected 1 arguments, but got 2. [plugin angular-compiler]JeanMeche commentedon Mar 3, 2024
@robertIsaac In your case you can fix it by specifying both generics defined by
viewChild.required<LocatorT, ReadT>(locator: ProviderToken<LocatorT>|string, opts: {read: ProviderToken<ReadT>}): Signal<ReadT>;:robertIsaac commentedon Mar 3, 2024
actually I was navigating through the Angular code trying to understand why and then I found it, was here to write that down but found you quickly answering
LayZeeDK commentedon Aug 15, 2024
Replacing the first signature overload of
injectangular/packages/core/src/di/injector_compatibility.ts
Line 144 in 5537bf8
with the followng works for
inject(ElementRef<HTMLElement>)but does not useProviderToken<T>.This is a noisy solution as it would have to be replicated for all
injectsignature overloads. We need a way to changeProviderToken<T>by usinginferbut I haven't been successful in achieving this so far.32 remaining items
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
fix(core): preserve generic type parameters when injecting or queryin…
refactor(core): preserve generic type parameters when injecting or qu…
refactor(core): add g3-only overloads to preserve generic token types
refactor(core): add g3-only overloads to preserve generic token types
refactor(core): add g3-only overloads to preserve generic token types
refactor(core): add g3-only overloads to preserve generic token types