I'm encountering an error in my Typescript code within an Angular project. The error message reads:
Object is possibly 'null'
This error occurs on the following line of code:
document.querySelector<HTMLElement>('.highlighted').style.backgroundColor = 'white';
If I modify the line to include a ?
like this:
document.querySelector<HTMLElement>('.highlighted')?.style.backgroundColor = 'white';
then a new error arises:
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
I am seeking assistance in resolving either of these errors.