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

137
Views
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 answers
Answer question

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 Report

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 Report
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!