Alan Omar
Alan Omar

Reputation: 4217

typescript Property 'formData' does not exist on type 'Event'?

I am getting this error message when trying to compile my typescript code:

typescript Property 'formData' does not exist on type 'Event'?
formElem.addEventListener("submit", (e) => {
    // on form submission, prevent default
    e.preventDefault();

    // construct a FormData object, which fires the formdata event
    new FormData(formElem);
});

formElem.addEventListener("formdata", (e) => {
    
    let data = e.formData;        // this is where the error occurs.
    .
    .
    })

I tried to search for an Event type which is a sub-type of Event but i was not lucky. What i am doing wrong here?Any Suggestions are welcome

Upvotes: 1

Views: 1985

Answers (1)

zhulien
zhulien

Reputation: 5695

I see you're using the sample from MDN's formdata event. The event type you're looking for is FormDataEvent which provides the formData property. If you check the browser compatibility table, however, you'll notice it is not supported in all browsers(namely Safari).

As I see, Typescript currently doesn't support this event type yet, but they have an open issue related to it here. You can verify it is missing in the general definitions as well.

At this stage, you either have to introduce a custom type(like the one in the issue provided above) or annotate the event as any. Once again, this won't work on Safari.

Here is a somewhat related question which may help you.

Upvotes: 1

Related Questions