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

164
Visualizações
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
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