Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

460
Views
Typescript: callback where event can be click or keydown throws Property 'key' does not exist on type 'Event'

I have a setup where click and keydown events call the same callback:

const onClick = (e: Event) => {
  callback(e);
};

const onKeydown = (e: KeyboardEvent) => {
  callback(e);
};

const callback = (e) => {
  if ('key' in e && e.key === 'Enter') console.log('yeet');
}    

I'm simplifying of course, but this is the essence of the setup.

yarn tsc is barking that Property 'key' does not exist on type 'Event'.

What's the right way to handle this situation? Any suggestions would be appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aha:

const onClick = (e: Event) => {
  callback(e);
};

const onKeydown = (e: KeyboardEvent) => {
  callback(e);
};

const callback = (e: Event | KeyboardEvent) => {
  if ('key' in e && e.key === 'Enter') console.log('yeet');
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!