Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get properties of a class

Is there a way to get properties names of class in TypeScript?

In the example, I would like to 'describe' the class A or any class and get an array of its properties (maybe only public ones?), is it possible? Or should I instantiate the object first?

class A {     private a1;     private a2;     /** Getters and Setters */  }  class Describer<E> {     toBeDescribed:E ;     describe(): Array<string> {         /**          * Do something with 'toBeDescribed'                                    */         return ['a1', 'a2']; //<- Example     } }  let describer = new Describer<A>(); let x= describer.describe(); /** x should be ['a1', 'a2'] */  
like image 605
timmz Avatar asked Nov 16 '16 15:11

timmz


People also ask

What is the properties of a class?

The collection of properties assigned to a class defines the class. A class can have multiple properties. For example, objects classified as computers have the following properties: Hardware ID, Manufacturer, Model, and Serial Number.

What are properties in code?

A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method.

What is GetProperties C#?

GetProperties(BindingFlags) MethodThis method is used to search for the properties of the current Type, using the specified binding constraints when overridden in a derived class. Syntax: public abstract System. Reflection. PropertyInfo[] GetProperties (System.


2 Answers

This TypeScript code

class A {     private a1;     public a2; } 

compiles to this JavaScript code

class A { } 

That's because properties in JavaScript start extisting only after they have some value. You have to assign the properties some value.

class A {     private a1 = "";     public a2 = ""; } 

it compiles to

class A {     constructor() {         this.a1 = "";         this.a2 = "";     } } 

Still, you cannot get the properties from mere class (you can get only methods from prototype). You must create an instance. Then you get the properties by calling Object.getOwnPropertyNames().

let a = new A(); let array = return Object.getOwnPropertyNames(a);  array[0] === "a1"; array[1] === "a2"; 

Applied to your example

class Describer {     static describe(instance): Array<string> {         return Object.getOwnPropertyNames(instance);     } }  let a = new A(); let x = Describer.describe(a); 
like image 181
Erik Cupal Avatar answered Sep 22 '22 14:09

Erik Cupal


Some answers are partially wrong, and some facts in them are partially wrong as well.

Answer your question: Yes! You can.

In Typescript

class A {     private a1;     private a2;   } 

Generates the following code in Javascript:

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

as @Erik_Cupal said, you could just do:

let a = new A(); let array = return Object.getOwnPropertyNames(a); 

But this is incomplete. What happens if your class has a custom constructor? You need to do a trick with Typescript because it will not compile. You need to assign as any:

let className:any = A; let a = new className();// the members will have value undefined 

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    } } 

For better understanding this will reference depending on the context.

like image 40
titusfx Avatar answered Sep 19 '22 14:09

titusfx