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

106
Views
La aserción de tipo funciona pero la anotación no con HTMLElement

Este código funciona:

 const a = document.getElementById("input1")! as HTMLInputElement

Sin embargo, este arroja un error:

 const a: HTMLInputElement = document.getElementById("input1")!

El error dice:

 Type 'HTMLElement' is missing the following properties from type 'HTMLInputElement': accept, align, alt, autocomplete, and 52 more

¿Alguien puede explicar por qué, por favor? ¿Significa esto que con la anotación de tipo aquí, tengo que especificar manualmente todas esas propiedades? Y si es así, ¿cómo lo haría?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Cuando realiza una consulta dom como document.getElementById("input1") , el tipo que obtiene es HTMLElement | null Con la ayuda de ! , le está diciendo a TypeScript que ignore la posibilidad de que sea null . Ahora la diferencia entre tus intentos es que:

  • const a = document.getElementById("input1")! as HTMLInputElement significa almacenar en a HTMLElement como HTMLInputElement . Con la inferencia de tipo de TypeScript, a sería de tipo HTMLInputElement .

  • const a: HTMLInputElement = document.getElementById("input1")! significa que está almacenando en una variable HTMLInputElement , un valor HTMLElement . Por eso te está gritando.

about 4 years ago · Juan Pablo Isaza 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!