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!
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');
}