Which fix removes the bug?
const el = document.querySelector('.box')!;
el.classList.add('on');
- Guard it:
if (el) el.classList.add('on') - Guard it:
if (el!) el.classList.add('on') - Change
querySelectortogetElementById - Cast the result to
as HTMLElementfirst
The ! only silences the type error; at runtime a missing element still throws.
Play Problem Solving ▸