Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda