Fix TypeScript error: Property ‘keyCode’ does not exist on type ‘FormEvent’.

Fix:
Change type to React.KeyboardEvent:

onKeyUp(event: React.KeyboardEvent<object>)

Error:

Property 'keyCode' does not exist on type 'FormEvent<object>'.

This code triggers the error:

onKeyUp(event: React.FormEvent&lt;object>) {
  event.preventDefault(); 
 
  if (event.keyCode === 13) {      
  }
}

Fixed:

onKeyUp(event: React.KeyboardEvent&lt;object>) {
  event.preventDefault(); 
 
  if (event.keyCode === 13) {      
  }
}

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *