Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • yeah, that might work, but we call that as inline typecasting, i.e., we're just changing type in place. still I feel, if there is null values, that is, if the div doesn't exist, it will still give null. and the typecasting null as HTMLElement might give error. did you try applying changes? Commented Feb 25, 2021 at 7:27
  • 1
    I did apply it and it seemed to be the only way to fix the error. And the cheatsheet gives more details as to why this method is recommended. Btw, the HTMLElement type is Element|null type so I don't think it'll give an error. I already tested it Commented Feb 26, 2021 at 10:47
  • I like this method because anyone coming across the code would clearly see what the type is. If you wanted, you could always combine the ? & the as keyword. So you could do something like const modalRoot = document.getElementById("modal-root") as HTMLElement; & then when you need to access it and its properties do: modalRoot?.foobar. Commented Nov 13, 2023 at 21:41