Avatar ·

How to fix error TS2533: "Object is possibly 'null' or 'undefined'" in TypeScript?

Hi everyone! I started learning TypeScript and encountered an error I can't understand. I have simple code where I get an element from the DOM and try to change its text: ```typescript const button = document.querySelector('.my-button'); button.textContent = 'Click me'; ``` TypeScript throws an error: ``` error TS2533: Object is possibly 'null' or 'undefined'. ``` I understand that `querySelector` can return `null` if the element is not found, but I know for sure that the element exists on the page. How can I tell TypeScript that I'm confident the element exists? I tried adding checks, but I'm still confused. Please advise what ways there are to solve this problem? It's important for me that the code remains safe, but at the same time doesn't get cluttered with unnecessary checks.