Flow type refinement in events

129 Views Asked by At

Well I have an event that happens "onchange" from a text input.

One of the obvious fields I would like to get during event handling is the new value of the input. A simple example is shown below:

onChange = (event: SyntheticInputEvent<>) => {
    const target = event.currentTarget;
    const {something} = this.props;

    if (target && target.hasOwnProperty('value')) {
        something[fieldname] = target.value;
    }
};

This however chokes flow: flow complains that about:

Error:(34, 47) Cannot get target.value because property value is missing in EventTarget [1].

Now how can I correctly refine this type? Apparently flow doesn't understand hasOwnProperty. Is there a better way?

1

There are 1 best solutions below

0
On

The SyntheticInputEvent generic requires type argument, you seems to missed it.

Smth, like this should make flow happy:

const onChange = (event: SyntheticInputEvent<HTMLInputElement>) => {
    const target = event.currentTarget;
    console.log(target.value);
};