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 :)

3
  • 3
    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. Commented Oct 4, 2021 at 16:28
  • Gotcha, Thanks! Commented Oct 4, 2021 at 16:44
  • You can also allow implicit any. This is useful when you are migrating legacy code into your application. Commented Aug 21, 2023 at 14:38

2 Answers 2

-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"))
}
Sign up to request clarification or add additional context in comments.

Comments

-2

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);
};

5 Comments

How would you go about giving it an actual type?
Instead of event:any, you would replace any with the type. For example, event: Event
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'.
Yea those would be other typescript issues. But if you get those errors, it means you have solved the issues you posted
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.

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.