0

I have no errors in syntax (event.target as HTMLInputElement), and Typescript checks file as A-OK, but it fails to compile, and ends with an error. I am using React 18 alpha with Typescript support (app created with create-react-app).

This is the function:

const handleOnKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
  if (event.code === "Enter") {
    navigate(api.searchEndpoint + (event.target as HTMLInputElement).value);
  }
};

Everything is OK and green in VS Code, and the syntax is ok, but when I try to compile it, i got this error:

src/components/elements/Search.tsx
  Line 16:51:  Parsing error: Unexpected token, expected ","

  14 |   const handleOnKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
  15 |     if (event.code === "Enter") {
> 16 |       navigate(api.searchEndpoint + (event.target as HTMLInputElement).value);

Does somebody know what is the problem??

Manzana
  • 129
  • 1
  • 9
  • I can't reproduce this in codesandbox.io. Do you have a minimal reproducible example? – Summer Dec 28 '21 at 19:18
  • @Summer I think than it will work in code sandbox... I think that my Babel configuration is causing this error... – Manzana Dec 28 '21 at 19:32
  • Ok, I found out that this is asked before... No answer, though... https://stackoverflow.com/questions/64717567/typescript-in-a-react-project-is-no-longer-recognizing-the-as-keyword – Manzana Dec 28 '21 at 19:38

0 Answers0