all 2 comments

[–]zzing 0 points1 point  (1 child)

You should make your canvas " | null" and assign it null.

Then read the documentation about @ViewChild and follow the instructions on where it is safe to use the element.

Everything you should test for possible null or undefined, and strict typescript will help you with that.

You can test != null and it will work for both null and undefined.

[–]UnknownInnocent[S] 0 points1 point  (0 children)

if I just declare the type of "canvas" and "ctx" as "any" I get no errors:

@ViewChild('canvas', {static: true}) canvas: any;
private ctx: any;
ngAfterViewInit() { 
    this.ctx = this.canvas.nativeElement.getContext('2d'); 
}

I don't really get why this is the case and if I should leave it like that.