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
Sandhiya PriyaPosted Oct 15, 2025, 12:18 PM
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)
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.Cynthia SathuragiriPosted Oct 8, 2025, 5:17 AM
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.
Amira BedhiafiPosted Oct 1, 2025, 2:03 PM
Hello !
Your index should be like below :
and for the counter :