Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc: Property 'value' does not exist on type 'EventTarget'

I'm using vanilla JavaScript with TypeScript as pre-processor in combination with JSDoc.
That pretty much works flawlessly, especially in the backend (when using in NodeJS, for instance).

However, when I use it with DOM objects, things get a bit tricky.

For example: Say I have an HTML Input field, I catch the input event and want to access the input's value with e.target.value:

/**
 * Log data on input
 *
 * @param {Event} e
 */
let handleEvent = function(e){
    console.log(e.target.value);
};

document.getElementById("my-input").addEventListener("input", handleEvent);

This results in a TS warning:

Property 'value' does not exist on type 'EventTarget'

As seen here:

TypeScript Warning

Now my question is, what's the correct @param annotation?

So far I've tried Event, InputEvent and HTMLInputElement.

I don't want to use Type-Assertion. Instead I'd like to know how to specify it in the functions annotations directly, so I do not have to set @type for each and every occurrence of e.target.value individually as suggested here.

like image 432
NullDev Avatar asked Feb 15 '21 10:02

NullDev


People also ask

Why does the error'value'does not exist on type'eventtarget'?

The error "Property 'value' does not exist on type 'EventTarget'" occurs when we try to access the value property on an element that has a type of EventTarget. To solve the error, use a type assertion to type the element correctly before accessing the property.

What is “property ‘property name’ not existing on eventtarget” in typescript?

A common error that occurs when working with the DOM in TypeScript is “property ‘property name’ not existing on EventTarget ”. This error occurs when you try to access a property on an event target in TypeScript. Property 'value' does not exist on type 'EventTarget'.

What is ‘value does not exist on type ‘eventtarget’ error in angular?

Property ‘value’ does not exist on type ‘EventTarget’ Error is a common error in Angular. It shows up when you try to access the value property of an HTML element during event binding. This error is flagged by the TypeScript compiler. The $event in both examples are of type HTMLElement. It can represent any HTML Element.

Why is my event target object not working?

The first error occurs because the type of the target object is EventTarget | null and we’re trying to access a property on a nullable type which is not allowed in TypeScript (if strictNullChecks is enabled). We can fix this error by narrowing the type to just EventTarget through a type guard:


1 Answers

You can write your @param annotation like that:

/**
 * Log data on input
 *
 * @param {Event & { target: HTMLInputElement }} e
 */
let handleEvent = function(e){
    console.log(e.target.value);
};

Then you get full support for the correct types:

1

The reason why this works, is that you're basically "extending" the Event class by the new target property - which already exists, so it gets overridden with our new type.

The ampersand per se is not a jsdoc operator, but one from TypeScript. So this only works out if you keep using TypeScript as your preprocessor. See this for a more detailed explanation.

like image 162
CreepSore Avatar answered Sep 22 '22 11:09

CreepSore