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

141
Vistas
Listing all possible properties of a class in javascript

I want to list all possible class properties. Something like this:

class Rectangle {
  height;
  width;
  constructor(width) {
    this.width = width;
  }
}

getClassProperties(Rectangle) // ['height', 'width']

I want to be able to make this calculation give the class, not an instance of it. using hasOwnProperty \ getOwnPropertyName + getPrototypeOf will only work on an instance.

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

ES6 classes are a "syntactic sugar" method and are essentially functions. In order to get the list of properties you have to run the function i.e. instantiate the object.

class Rectangle {
  height;
  width;
  constructor(width) {
    this.width = width;
  }
}

function getClassProperties(jsClass) {
  if (typeof jsClass !== "function") {
    throw Error("not a class");
  }

  return Object.keys(new jsClass);
}

console.log(getClassProperties(Rectangle));

about 4 years ago · Santiago Gelvez Denunciar

0

Modified answer from: https://stackoverflow.com/a/46237515/17954209

A general solution will be:

class A {
    private a1;
    private a2;
    constructor(a1:number, a2:string){
        this.a1 = a1;
        this.a2 = a2;
    } 
}

class Describer{    
   describeClass(typeOfClass: any){
       let a = new typeOfClass();
       let array = Object.getOwnPropertyNames(a);
       return array;//you can apply any filter here    
   }
}

Or alternatively in a function form

function getClassProperties(typeOfClass: any) {
    let a = new typeOfClass();
    let array = Object.getOwnPropertyNames(a);
    return array; //you can apply any filter here   
}
// You tagged typescript in the question, but javascript in the actual title, 
// so here's the JS equivalent
function getClassProperties(typeOfClass) {
    let a = new typeOfClass();
    let array = Object.getOwnPropertyNames(a);
    return array; //you can apply any filter here   
}

Basically at some point you'll have to create an instance of the class in order to run the constructor function to initialize the properties. You either have to initialize the property in the constructor, or while declaring it. Otherwise it will not work. It's explained well in the linked answer, quoted below. If you really don't define the property in the constructor but would like a workaround, simply set it to undefined or null in the constructor/declaration.

In Typescript

class A {
    private a1;
    private a2;


} 

Generates the following code in Javascript:

var A = /** @class */ (function () {
    function A() {
    }
    return A; }());

I would recommend reading more of the answer I modified from if you have any other questions.

If you want Typescript to give a return type as well you can use the handy ConstructorParameters utility type

function getClassProperties<T extends {new (...args: any): any}>(typeOfClass: T) {
    let a = new typeOfClass();
    let array = Object.getOwnPropertyNames(a);
    return array as ConstructorParameters<T>;
}

View on TS Playground

about 4 years ago · Santiago Gelvez 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