1

I am getting Parameter 'event' implicitly has an 'any' type.ts(7006) from the code below -

const handleFormSubmit = (event) => {
    event.preventDefault();

    const email = event.target.elements.email?.value;
    const password = event.target.elements.password?.value;

    console.log(email, password);
};

Searched around but didn't found any answer which can solve it, any help is appreciated. Thank you :)

Priyam
  • 70
  • 1
  • 5
  • 2
    The type is being inferred here because you did not declare it, therefore it defaults to `any`. You must declare the type, such as `event: Event`. In a TypeScript environment -- by default -- you must strongly declare function arguments typings. – Ohgodwhy Oct 04 '21 at 16:28
  • Gotcha, Thanks! – Priyam Oct 04 '21 at 16:44
  • You can also allow `implicit any`. This is useful when you are migrating legacy code into your application. – Steven Spungin Aug 21 '23 at 14:38

2 Answers2

0

This is a typescript error. You can explicitly give it an any type. Or give it an actual type. Or change your typescript settings to allow implicit any.

const handleFormSubmit = (event:any) => {
    event.preventDefault();

    const email = event.target.elements.email?.value;
    const password = event.target.elements.password?.value;

    console.log(email, password);
};
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
  • How would you go about giving it an actual type? – Tzane Aug 21 '23 at 11:05
  • Instead of `event:any`, you would replace `any` with the type. For example, `event: Event` – Steven Spungin Aug 21 '23 at 14:36
  • Sure, but that just leads to other typescript errors, if you want to access `event.target.elements.email?.value`: `'event.target' is possibly 'null'.` and `Property 'elements' does not exist on type 'EventTarget'.` – Tzane Aug 21 '23 at 15:08
  • Yea those would be other typescript issues. But if you get those errors, it means you have solved the issues you posted – Steven Spungin Aug 21 '23 at 17:03
  • 1
    You probably got downvotes because your issue has plenty of related answers already. Its a common issue. I have found it sometimes makes sense to turn off ALL the typescript strict checks, then turn them on one at a time. – Steven Spungin Aug 22 '23 at 02:28
-1

I tried casting the event.target as HTMLInputElement as suggested here, but I still got complaints for missing parameters. I ended up doing this in the end

async function newPlayer(event: Event) {
  event.preventDefault();
  const form = event.target as HTMLFormElement;
  const formData = new FormData(form);
  console.log(formData.get("name"))
}
Tzane
  • 2,752
  • 1
  • 10
  • 21