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.
Typescript would help you so much at these times.......
– Cmte Cardeal