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

129
Views
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 answers
Answer question

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 Report

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 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!