Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda