Selecting elements
DOM selectors may fail, so TypeScript correctly includes null in their result. Check first instead of using a type assertion to silence the warning.
TypeScript
const button = document.querySelector<HTMLButtonElement>("#save"); if (button) { button.disabled = true; }
Handling null
The check is not ceremony: it records that an absent element is expected and dealt with. If an element is genuinely required, throw a helpful error close to the selector rather than using ! throughout the codebase.
Did you know my courses are all printer-friendly? This means you can easily export them to PDF or print them for later use.
