Skip to content

viewChild function doesn't accept generic type for ElementRef #54645

@json-derulo

Description

@json-derulo

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

I just tried out the new viewChild() function, with the { read: ElementRef } option. Usually I use ElementRef with a generic type, i.e. ElementRef<HTMLElement> to have better type safety. I tried to set a generic type:

myViewChild = viewChild(MyComponent, { read: ElementRef<HTMLElement> });

But this throws me an error:

TS-991011: Query "read" option expected a literal class reference. [plugin angular-compiler]

After removing the generic type, the error is gone, however the signal will infer type Signal<ElementRef<any> | undefined>. I want to replace the any type with a proper type.

Proposed solution

The viewChild() function offers a possibility to set the generic type of the ElementRef.

Alternatives considered

For now I'm overriding the type of the signal, but this looks quite verbose:

myViewChild: Signal<ElementRef<HTMLElement> | undefined> = viewChild(MyComponent, { read: ElementRef });

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions