20

I don't quite understand how event typing works, I'd like to specify a type here, but cannot quite figure out how to do it. I cannot seem to find a type reference for this specific case.

private handleChange (event /*:FileUplaodEvent or something */): void {
    this.setState ({
      csv: event.target.files[0],
    });
}

Any help would be appreciated...

edit: As answer by Mukesh Soni stated I used React.FormEvent<HTMLInputElement>, but for whatever reason the interface for files is a bit different for this type, it is actually: event.currentTarget.files rather than event.target.files.

2 Answers 2

39

The accepted answer didn't work for me.

If you want to access e.target.files or e.currentTarget.files You should use React.ChangeEvent<HTMLInputElement> as the event type .

Example:

const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
    if (!e.target.files) {
      return;
    }

    // handle the input...
    console.log(e.target.files);
}


<Input type='file' onChange={handleChange}/>
Sign up to request clarification or add additional context in comments.

Comments

10

The event argument for your input's onChange is actually react's synthetic event.

React synthetic event types are present on React object (assuming you have installed types for react).

(event: React.FormEvent<HTMLInputElement>)

React.FormEvent consists of all syhthetic events on form input elements. It is a generic type, i.e. accepts another type as an argument when you use it. In your case, that type argument is HTMLInputElement. Which results in a complete type of a form event on input element.

1 Comment

I didn't see it directly, so I repeat the edit from the question here: 'the interface for files is a bit different for this type, it is actually: event.currentTarget.files rather than event.target.files.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.