Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property 'submitter' does not exist on type 'Event'

Banging my head against the wall on this one…

given this function

const handleFormSubmitByButton = (event: FormEvent<HTMLFormElement>) => {
  event.preventDefault();
  const submitter  = event.nativeEvent.submitter;
  
  console.log(submitter?.value)
};

and markup

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<form onSubmit={handleFormSubmitByButton}>
  <button type="submit" name="test" value="test">Submit</button>
</form>

Why am I keep getting Property 'submitter' does not exist on type 'Event'.?

I tried to use SyntheticEvent from React like so: SyntheticEvent<HTMLFormElement, SubmitEvent>

This yields submitter being okay but then complaining about Property 'value' does not exist on type 'HTMLElement'.

As a last result, know it won’t work I did also try to type cast on the declaration of submitter. :S

TS version in project is 4.9.5

>Solution :

You need to add guard clauses:

const handleFormSubmitByButton = (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault();

    if (!(event.nativeEvent instanceof SubmitEvent)) return;

    const submitter = event.nativeEvent.submitter;

    if (!(submitter instanceof HTMLInputElement)) return;

    console.log(submitter.value); // ok
};

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading