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

Published on · Updated on
3 min read.
Tags: JavaScriptTypeScript

TypeScript throws this when you read event.target.value and the type of event.target is plain EventTarget:

Property 'value' does not exist on type 'EventTarget'.ts(2339)

EventTarget is the base interface every DOM node, window and XMLHttpRequest share. It only has addEventListener, removeEventListener and dispatchEvent, so value, checked and files aren't on it.

Why target isn't typed as your input

Events bubble. A handler on a <form> or <div> also fires for clicks and keystrokes on its children, so event.target can be any descendant. TypeScript can't know which one, and types it as EventTarget.

event.currentTarget is the element the handler is attached to, which TypeScript does know. That's why React's types give it your element type:

function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
  event.target.value;        // error TS2339
  event.currentTarget.value; // string
}

The same applies to React.FormEvent, React.MouseEvent and the other React event types. The exception is React.ChangeEvent<T>: React's types narrow target to your element too, so event.target.value compiles there.

Fix it in React

For onChange, type the event as React.ChangeEvent with the element:

function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
  console.log(event.target.value);
}

function handleSelect(event: React.ChangeEvent<HTMLSelectElement>) {
  console.log(event.target.value);
}

If you write the handler inline, <input onChange={(e) => e.target.value} />, TypeScript infers that type for you.

For every other event (onInput, onKeyDown, onClick, onSubmit), read currentTarget instead of target:

function handleInput(event: React.FormEvent<HTMLInputElement>) {
  console.log(event.currentTarget.value);
}

On a form's onSubmit, event.target is still EventTarget. Use currentTarget to get the form and read the fields with FormData:

function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
  event.preventDefault();
  const data = new FormData(event.currentTarget);
  console.log(data.get("email"));
}

Fix it with addEventListener

Native DOM events have no type parameter, and target is EventTarget | null, so you get two errors:

input.addEventListener("input", (event) => {
  event.target.value;
  // error TS18047: 'event.target' is possibly 'null'.
  // error TS2339: Property 'value' does not exist on type 'EventTarget'.
});

Narrow it with instanceof. That handles null and checks the element type at runtime:

input.addEventListener("input", (event) => {
  if (event.target instanceof HTMLInputElement) {
    console.log(event.target.value);
  }
});

If the listener can only ever fire on an input, a cast is shorter but skips the runtime check:

const value = (event.target as HTMLInputElement).value;

Don't use event.target!.value. The ! removes the null error, but TS2339 stays because the type is still EventTarget.

Which one to use

HandlerRead the value from
React onChange with ChangeEvent<T>event.target.value
Any other React eventevent.currentTarget.value
React onSubmitnew FormData(event.currentTarget)
addEventListenerinstanceof check, then event.target.value