Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

952
Views
La propiedad 'valor' no existe en el tipo EventTarget en TypeScript

Entonces, el siguiente código está en Angular 4 y no puedo entender por qué no funciona como se esperaba.

Aquí hay un fragmento de mi controlador:

 onUpdatingServerName(event: Event) { console.log(event); this.newserverName = event.target.value; //this wont work }

elemento HTML:

 <input type="text" class="form-control" (input)="onUpdatingServerName($event)">

El código me da el error:

La propiedad 'valor' no existe en el tipo 'EventTarget'.

Pero como se puede ver en console.log , ese valor existe en event.target .

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

event.target aquí es un HTMLElement que es el padre de todos los elementos HTML, pero no se garantiza que tenga el value de la propiedad. TypeScript detecta esto y arroja el error. Transmita event.target al elemento HTML apropiado para asegurarse de que sea HTMLInputElement que tenga una propiedad de value :

 (<HTMLInputElement>event.target).value

Según la documentación :

Escriba el $event

El ejemplo anterior convierte el $event como any tipo. Eso simplifica el código a un costo. No hay información de tipo que pueda revelar las propiedades del objeto de evento y evitar errores tontos.

[...]

El $event ahora es un KeyboardEvent específico. No todos los elementos tienen una propiedad de value , por lo que convierte target en un elemento de entrada.

(Énfasis mío)

over 4 years ago · Santiago Trujillo Report

0

Pasar HTMLInputElement como genérico al tipo de evento también debería funcionar:

 onUpdatingServerName(event: React.ChangeEvent<HTMLInputElement>) { console.log(event); this.newserverName = event.target.value; }
over 4 years ago · Santiago Trujillo Report

0

Aquí hay otra solución que funciona para mí:

(event.target as HTMLInputElement).value

Eso debería eliminar el error al informar a TS que event.target es un HTMLInputElement , que inherentemente tiene un value . Antes de especificar, es probable que TS solo supiera que el event solo era un HTMLInputElement , por lo tanto, según TS, el target ingresado era un valor asignado aleatoriamente que podría ser cualquier cosa.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!