Search code examples
javascriptintellij-idea

How to tell IntelliJ about javascript event properties so it stops warning about undefined props?


IntelliJ seems to constantly complain about "Unresolved variable" for JavaScript.

For example, in

document.getElementById('myId').onclick = async event => {
    if (event.target.classList.contains...

it will underline classList with a warning "Unresolved variable classList"

classList is a well defined property. But an EventTarget could in theory be other things. Is there a "right" way to code or annotate it such that IntelliJ doesn't complain?


Solution

  • The IDE has no idea what your target element is, and EventTarget interface doesn't have classList property. You need to explicitly tell the IDE the type of the HTMLElement which is your target. For example:

    document.getElementById('myId').onclick = async event => {
      const target = /** Element */ event.target;
      if (target.classList.contains