#Expression "1" cannot be used to index the type "EventTarget

7 messages · Page 1 of 1 (latest)

pearl raven
#

hi i have a problem. I have this function:

    evt.preventDefault()
    const { target } = evt
    const description = target[1].value
  }```

and i got this error.
`The element has an implicit type of "any" because the type expression "1" cannot be used to index the type "EventTarget"` Someone know how can i solve?
latent iron
#

@pearl raven You'll get better types if you use currentTarget, not target.

#

currentTarget is guaranteed to be the element the event is attached to, target could be a different element, due to bubbling.

#

You'll probably still need to do some cast on target[1] since that would be Element, a generic element, and you seem to be expecting it to be some sort of input element.

pearl raven
#

thx. for the recommendation

#

@latent iron i used this const description = (currentTarget[0] as HTMLInputElement).value and the error dissapear, but its a good solution?

latent iron
#

Yeah, that's fine. as is making a "dangerous" assumption there, but I'm not sure how else you'd do it realistically.