I have two file now one is html and one is ts file. upon build js file is creating . but its not working in web browser.
Index.html file..
Counter.ts file...This below code is not working..
var inc=document.getElementById('increment') as HTMLButtonElement
inc.addEventListener("click",test)
function test():void{
console.log("Hello")
}
Even the test function showing duplicate function implementation

Amira BedhiafiRank #601 · 4K reputationPosted Oct 1, 2025, 10:03 AM EDT
Hello !
Your index should be like below :
and for the counter :
// Wait for the DOM to load before querying window.addEventListener("DOMContentLoaded", () => { const inc = document.getElementById("increment") as HTMLButtonElement | null; if (inc) { inc.addEventListener("click", () => { console.log("Hello"); }); } else { console.error("Button with id 'increment' not found"); } });Cynthia SathuragiriRank #937 · 2.2K reputationPosted Oct 8, 2025, 1:17 AM EDT
const button = document.getElementById("increment") as HTMLButtonElement;
button?.addEventListener("click", () => {
console.log("Hello from TypeScript!");
});
Make sure you:
Run tsc Counter.ts to generate Counter.js.
Include Counter.js in your HTML (after the button).
project/
¦
+-- index.html
+-- Counter.ts
+-- Counter.js (generated by tsc)
If you still see no message in the console, open Developer Tools (F12) → Console tab, and check for any error like "Cannot read property 'addEventListener' of null".
That means your button wasn’t found — and wrapping the code in DOMContentLoaded fixes that.
Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 15, 2025, 8:18 AM EDT
Your code is correct in logic, but a few common mistakes can cause it to “not work” in the browser.
1. Correct TypeScript Code (Counter.ts)
// Counter.ts // Wait until the DOM is ready before trying to access elements window.addEventListener("DOMContentLoaded", () => { const inc = document.getElementById("increment") as HTMLButtonElement | null; if (inc) { inc.addEventListener("click", test); } }); function test(): void { console.log("Hello"); }2. HTML File (index.html)
Make sure your compiled JavaScript file (usually
Counter.js) is loaded properly in the HTML.3.Compile TypeScript to JavaScript
Run this in your terminal:
That generates
Counter.jsin the same folder.If you use a TypeScript config file, you can run:
to auto-compile on save.
4. Common Mistakes That Break It
to end ofor wrap inDOMContentLoadedCounter.js)tsc Counter.tsfirstfunction test()definition allowedWhy “Duplicate function implementation” error appears
That usually happens when:
You have multiple
.tsfiles with the samefunction test()in the same namespace (without modules).Or you included both
Counter.tsandCounter.jsin your project reference list.