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

162
Views
Why 'this' is undefined in TypeScript?

I have code example:

export {};
class Userr{
    name : string;
    constructor(_name:string){
          
        this.name = _name;
    }
}


const Tom : Userr = new Userr("Tom");

const header = this.document.getElementById("header");

header.innerHTML = "Wassup " + Tom.name;

In JS it looks like:

"use strict";
exports.__esModule = true;
var Userr = /** @class */ (function () {
    function Userr(_name) {
        this.name = _name;
    }
    return Userr;
}());
var Tom = new Userr("Tom");
var header = this.document.getElementById("header");
header.innerHTML = "Wassup " + Tom.name;

IDE (VS Code) tells that in .ts file I have an error:
error TS2532: Object is possibly 'undefined'. 13 const header = this.document.getElementById("header"); Why? What should I do, to get proper access to the document? index.html for more context:

<!DOCTYPE html>
<html>
   
<head>
    <meta charset="utf-8" />
    <title>Just title</title>
    
</head>
<body>
    <h2 id="header"></h2>
    <script src="app.js"></script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
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!