1

I am getting a typescript error when trying to reset a form when the user clicks on reset button.

onClearSearchFormClicked() {
  this.$refs.searchForm.reset();
}
197:27 Property 'reset' does not exist on type 'Element | Element[] | Vue | Vue[]'.
  Property 'reset' does not exist on type 'Element'.
    196 |   onClearSearchFormClicked() {
  > 197 |     this.$refs.searchForm.reset();
        |                           ^
    198 |   }
Version: typescript 3.8.3

2 Answers 2

2

I wouldn't advise the other answers. TypeScript is correctly telling you that the object could not be a HtmlFormElement. Instead of forcing the casting and suppressing the error, you'd rather use a type guard:

if (this.$refs.searchForm instanceof HTMLFormElement) {
  this.$refs.searchForm.clear();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is the code that worked.

  onClearSearchFormClicked() {
    //this.$refs.searchForm.reset();
    (this.$refs.searchForm as HTMLFormElement).reset();
  }

1 Comment

No worries, I saw you're new, which is why I told you you shouldn't have done it. In general, if someone answers your question and helps you solve your issue, you should upvote it and, eventually mark it as appropriate (accepted) answer. That will help future users with a similar problem spot the right answer faster. On the bright side, Cristian added a better answer than mine, which is why I deleted mine and upvoted his. If I were you, I'd mark it as accepted, as it is correct. Welcome to Stack Overflow. Stay safe & happy coding!

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.