This should be extremely easy but apprently not.
In a standard HTML input
element, I would like to perform and action if Enter
is hit.
I am trying to learn TS and cannot figure out which type fits everything I need.
<input type="text" onChange={handleInput} />
const handleInput = (event: any) => {
if (event.key === 'Enter') {
console.log(event.target.value);
};
};
- If
event: InputEvent
then I can't even call the function, because the event type is wholly incompatible. - If
event: ChangeEvent<HTMLInputEvent>
then I can reachevent.target.value
, but an error says there is nokey
property, which is correct. In this case, the keystroke value is kept underevent.nativeEvent.data
. However, if I go forif (event.nativeEvent.data...)
then an error says there is nodata
property, even though there clearly is:
// from the console
nativeEvent: InputEvent {isTrusted: true, data: "d"...)
- If
event: KeyboardEvent
then I getevent.key
but nowevent.target.value
doesn't exist.
I would love to know two things:
- What is the correct type for this event?
- Where can I learn about the endless amount of types for these minor differences?