document.getElementById can return any HTML element.
However, note type assertions tell TypeScript, "I know what I'm doing". What if that is no longer true? What if we decide
main-content is no longer a
<canvas> but now an
<div>? The code would fail, but it would fail at the time in which
getContext is called. What if
getContext is in another module?
That error message is confusing. It would be nice if the error was closer to the source of the issue.
The strange type
new () => T simply means "a class of type T". The magic part is
asserts value is T. This is a promise to TypeScript that, "This function will assert value is type T and will throw an error otherwise"
main-content becomes a
<div>, an error is thrown.
Excellent! The error is now close to where canvas is first declared making it far easier to fix.
Do you want to write honest TypeScript? You're in luck, Battlefy is hiring.