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

130
Vistas
How can I convert JS to TS?

I read many articles about TypeScript and couldn't find anything. What type to set for elements X and Y? or how to convert the code to TS? thanks in advance for your help

const x = document.getElementById("password-input");
const y = document.getElementById("img");
function showPass() {
            if (x.type === "password") {
            x.type = "text";
            y.src = "png"

        } else {
            x.type = "password";
            y.src = "png"
        }
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

With the ! sign you said that this element won't be null (if you are sure that element is in your HTML document), and as HTMLInputElemnt is for indicating your element type.

To read more about it, you can read this article.

const x = document.getElementById("password-input")! as HTMLInputElement;
const y = document.getElementById("img")! as HTMLInputElement;

function showPass() {
            if (x.type === "password") {
            x.type = "text";
            y.src = "png"

        } else {
            x.type = "password";
            y.src = "png"
        }
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can't just use TS in the browser, you'll need to compile it. For your specific use case, you will need to tell TS what the HTMLElement is for x and y, otherwise it won't like x.type and y.src as src and type aren't properties on HTMLElement

const x: HTMLInputElement = document.getElementById("password-input");
const y: HTMLImageElement = document.getElementById("img");
function showPass() {
            if (x.type === "password") {
            x.type = "text";
            y.src = "png"

        } else {
            x.type = "password";
            y.src = "png"
        }
    }
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