Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
why typescript throws Binding element 'target' implicitly has an 'any' type.ts(7031)?

I'm trying to get the correct type for this function parameter

  const handleInputChange = ({ target }) => {
    setFormValues({
      ...formValues,
      [target.name]: target.value,
    });
  };

its throwing me an error that didn't crash the app but I know its not a good practice.

to give more context, all the fields that are changing are strings.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's telling you that because you haven't told it what the type of the parameter is.

You need to tell it what the type is. There are a couple of ways:

  1. Type the constant you're assigning the function to; or

  2. Directly type the parameter

Type the constant you're assigning the function to

You can do that with the ChangeEventHandler type, which is generic, taking the type of the element as a type argument:

import { ChangeEventHandler } from "react";

// ...

const onChange: ChangeEventHandler<HTMLInputElement> = ({currentTarget}) => {
// type −−−−−−^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    console.log(`${currentTarget.name} => ${currentTarget.value}`);
};

Directly type the parameter

The parameter is an event object which has a currentTarget property, so if (for instance) this is being used on an HTMLInputElement, you can use the type { currentTarget: HTMLInputElement }:

const handleInputChange = ({ currentTarget }: { currentTarget: HTMLInputElement }) => {
// type −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    // ...
};

Or you can use ChangeEvent<T>:

import { ChangeEvent } from "react";

// ...

const handleInputChange = ({ currentTarget }: ChangeEvent<HTMLInputElement>) => {
// type −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    // ...
};

Note I used currentTarget rather than target. It doesn't usually matter for input elements (target and currentTarget are always be the same if the handler is on the input, not its parent/ancestor), but it does for button elements and some others (since target might be a descendant element). And it matters if you're handling change on a parent/ancestor element instead of on the input directly.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda