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

166
Visualizações
Typescript properties array of class

I'm trying to loop on each properties of class from a new object, without any property set. Object.keys() return an empty array. Maybe because none of properties are set. Do you know a way to loop on properties of a class or a fresh new object of this class ?

export class Perimeter {
  Id: string;
  Name: string;
  Description: string;
  Typapp: string;
}

 var tmp = new Perimeter();
 console.log('Object keys tmp', Object.keys(tmp)); //Object keys tmp []

Result that I expect is ['Id', 'Name', 'Description', 'Typapp']

Thank you in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is you're not initializing those properties, so they only exist on the type, not at runtime. TypeScript will be warning you about that, saying that they've never been definitely assigned. For example:

Property 'Id' has no initializer and is not definitely assigned in the constructor. (2564)

Example

You need to initialize the properties for them to exist on the instance you're passing to Object.keys. You might do that in a couple of different ways. One of them is to use the public modifier on constructor parameters, which both creates a public property and automatically assigns the parameter's value to that property:

/*export*/ class Perimeter {
    constructor(
        public Id = "",
        public Name = "",
        public Description = "",
        public Typapp = ""
    ) {
    }
}

const tmp = new Perimeter();
console.log('Object keys tmp', Object.keys(tmp)); //Object keys tmp []

Live Example

(I've included default values there because you've shown a call to the constructor that doesn't provide any arguments.)

But you can also write it explicitly:

/*export*/ class Perimeter {
    Id: string;
    Name: string;
    Description: string;
    Typapp: string;
    constructor(id = "", name = "", description = "", typapp = "") {
        this.Id = id;
        this.Name = name;
        this.Description = description;
        this.Typapp = typapp;
    }
}

const tmp = new Perimeter();
console.log('Object keys tmp', Object.keys(tmp)); //Object keys tmp []

Live Example

For a class that has both properties with fixed initial values and ones that are assigned in the constructor, I prefer to both declare them and then assign the ones that need it in the constructor, but for ones where all of the properties are constructor parameters, it makes for a handy shorthand. Either way, it's a matter of style.

about 4 years ago · Juan Pablo Isaza Relatório
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