class SnakeGame {
private canvas: HTMLCanvasElement // Property 'canvas' has no initializer and is not definitely assigned in the constructor.ts(2564)
constructor(canvasId: string) {
let canvas = document.getElementById(canvasId)
if (canvas instanceof HTMLCanvasElement) {
this.canvas = canvas
}
else {
throw new Error('Canvas is not an HTMLCanvasElement')
}
}
}
Would you type canvas with | null in this case ? I feel like it's not optimal because it will force to check canvas !== null in every function that use it...