VS Code does not recognize methods of the Event object

Asked

Viewed 30 times

1

My VS Code does not recognize the methods and parameters of the Event object when it is sent by an anonymous function as a parameter of another function.

In this case you see it working.

This one doesn’t work.

I’ve installed almost all VS extensions and don’t know why it doesn’t work. If anyone can help.

Extra information: the code itself doesn’t matter, just the problem with the "e". The console.log(e) return to Event, no Undefined problem.

  • 1

    Typescript would help you so much at these times.......

1 answer

3


Even Typescript would not help in this kind of situation. There is a lack of information required for "inference" of types.

The problem is that the compiler (used internally by Vscode) does not have enough information to know the type of the parameter e.

Therefore, until you add the information necessary to determine the type of that parameter, there will be nothing to "infer", since the caller can, in theory, pass anything as an argument there.

In Typescript, this information could be added with an explicit type annotation. For example:

function handleEvent(e: Event) {
    console.log(e.target);
}

See working on Typescript playground.

If you are not using Typescript, you can add this type of information using Jsdoc comments. Vscode uses the Typescript infrastructure to handle the types reported via Jsdoc, so the effect will be virtually the same in editors that support it.

Thus:

/**
 * @param {Event} e
 */
function handleEvent(e) {
    console.log(e.target);
}

See working on Typescript playground.

Learn more about Jsdoc on reference.

Note that in both cases (although in syntactically different ways), I added the type Event to the parameter e of function handleEvent.

The guy Event is the most generic for handlers event. There are others, such as MouseEvent, KeyboardEvent, SubmitEvent etc, which can be used to add more information about a specific type of event.

The full list is found in the official Typescript settings. Here.

  • Man, thank you so much. This is the first time I use Stackoverflow to ask a question and I don’t know exactly how I can "close" her or something. Regarding the answer I provisionally added the Jsdoc comment for this specific case and will study Typescript when I close my studies in pure Javascript. I know that the problem only affected my interaction with the code and not the code itself, but for those who are learning to have these aids it is a watershed. vlw even.

  • @Coriast, you are welcome! And welcome to Stack Overflow. :-) About "close" the question, you have already done what was necessary: as you found a satisfactory answer, just accept it by clicking on the check icon.

  • Even Typescript wouldn’t help in this kind of situation - I didn’t understand this line if soon after you gave an example with Typescript :/

  • @Cmtecardeal, I meant that even the Typescript language couldn’t help with just the code posted in the question. In the original AP code, as I said, information is missing so that the "Intelli-sense"be done. Of course with the type of the parameter reported, with or without Typescript, the editor would be able to determine the appropriate members.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.