7

If I have a component such as this, which references a native HTML element, what type annotation should be used?

@Component({
    template: `<video #test></video`
})
export class AppComponent {
    @ViewChild('test') test: any;

    constructor() {
        let nativeElement = this.test.nativeElement;
    }
}
Mike Jerred
  • 9,551
  • 5
  • 22
  • 42

2 Answers2

8

The ElementRef<> type should be used e.g.

@Component({
    template: `<video #test></video`
})
export class AppComponent {
    @ViewChild('test') test: ElementRef<HTMLVideoElement>;

    constructor() {
        let nativeElement = this.test.nativeElement;
        // nativeElement now has type HTMLVideoElement
    }
}
Mike Jerred
  • 9,551
  • 5
  • 22
  • 42
2

You can use ElementRef<typeoElement>(where typeOfElement is typo of element you are referring) This is show on answer above.

If the element you are accessing is not native element you can use the type of that component

e.g @ViewChild('test') test: ComponentName;

Angular docs contains a good example

alt255
  • 3,396
  • 2
  • 14
  • 20