Search code examples
javascripterror-handlingphpstormtarget

PhpStorm gives strange message


My PhpStorm gives a strange error message on my JavaScript code (I'm new to this). Anyway I am quite confident that my code is correct. But PhpStorm still gives me an error message

Unresolved variable valueAsDate

Any proposal what I can do better?

const startElement = document.querySelector('#date_start');

let startDate_oldValue;

startElement.addEventListener('focus', function (event) {
    console.log(event);
    startDate_oldValue = event.target.valueAsDate;
});

enter image description here


Solution

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

    startElement.addEventListener('focus', function (event) {
        console.log(event);
        const myEl = /** HTMLInputElement*/ event.target;
        startDate_oldValue = myEl.valueAsDate;
    });