question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(ElementRef): provide option to make generic

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] feature request

Current behavior

If I want to get a hold of a Custom Element by assigning an ElementRef via @ViewChild, the nativeElement property is typed as any instead of the Custom Element type.

@Component({
  template: `
    <my-element #my-element></my-element>
  `
})
class MyComponent {
  @ViewChild('my-element') myElement:ElementRef;
  ngAfterViewInit() {
    this.myElement.nativeElement; // type is "any"
  }
}

Expected behavior

@ViewChild('my-element') myElement:ElementRef<MyElement>;

Since TypeScript doesn’t support default generic types (see discussion, ElementRef would require a type be provided every time it’s used as a type annotation. Since this would be a breaking change, I propose subclassing ElementRef as CustomElementRef, and typing the nativeElement property as the generic type.

@ViewChild('my-element') myElement:CustomElementRef<MyElement>;

What is the motivation / use case for changing the behavior?

So that type information can be available when using custom elements (code completion, refactoring, all the usual goodies).

  • Language: [all | TypeScript X.X | ES6/7 | ES5]

TypeScript

CC @robdodson

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jeffbcrosscommented, Nov 30, 2016

Note: there are many workarounds to get type information without having a generic ElementRef; they’re just a little verbose.

  1. Create an alternate ElementRef interface:
interface MyCustomElementRef extends ElementRef {
  nativeElement: MyCustomElement;
}
...
@ViewChild('my-element') myEl:MyCustomElementRef;
  1. Cast nativeElement inline when accessing
@ViewChild('my-element') myEl: MyCustomElementRef
ngAfterViewInit() {
  (<MyCustomElement>this.myEl.nativeElement).foo();
}
  1. Inline a custom interface
@ViewChild('my-element') myEl: {nativeElement: CustomElement};
3reactions
lacolacocommented, Dec 9, 2016

I’m 👎 on T extends HTMLElement because ElementRef is not a thing only of browser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6 -"generic type elementref requires 2 type arguments ...
ElementRef expects a Type: T for it's nativeElement property: class ElementRef<T> { constructor(nativeElement: T) nativeElement: T }.
Read more >
Getting Element references (ElementRef) in Angular templates
Element references inside the template. First, let's create an <input> and give it an ngModel directive: <input ...
Read more >
Table of Contents
The Federation Engineering Agreements Template (FEAT) is intended to provide a standardized format for recording federation agreements to increase their ...
Read more >
thirtyfour_sync - Rust - Docs.rs
This crate provides a synchronous (i.e. not async) interface for thirtyfour . ... Advanced query interface featuring powerful filtering and polling options.
Read more >
ElementRef - Angular
Use this API as the last resort when direct access to DOM is needed. Use templating and data-binding provided by Angular instead. Alternatively...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found