Get properties of class in typescript

后端 未结 2 1760
有刺的猬
有刺的猬 2021-01-07 23:10

I\'ve the following class:

export class Test {

        private _rowsCount: string;
        public get RowsCount(): string {
            return this._rowsCou         


        
2条回答
  •  长情又很酷
    2021-01-07 23:53

    If you need to only get the getters/setters, then you'll need to do something like:

    class Test {
        ...
    
        public static getGetters(): string[] {
            return Object.keys(this.prototype).filter(name => {
                return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["get"] === "function"
            });
        }
    
        public static getSetters(): string[] {
            return Object.keys(this.prototype).filter(name => {
                return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["set"] === "function"
            });
        }
    }
    
    Test.getGetters(); // ["RowsCount", "RowsCount2"]
    Test.getSetters(); // ["RowsCount", "RowsCount2"]
    

    (code in playground)


    You can put the static methods in a base class, and then when you extend it the subclass will have those static methods as well:

    class Base {
        public static getGetters(): string[] {
            return Object.keys(this.prototype).filter(name => {
                return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["get"] === "function"
            });
        }
    
        public static getSetters(): string[] {
            return Object.keys(this.prototype).filter(name => {
                return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["set"] === "function"
            });
        }
    }
    
    class Test extends Base {
       ...
    }
    
    Test.getGetters(); // work the same
    

    (code in playground)

    If you want these methods to be instance methods then you can do this:

    class Base {
        public getGetters(): string[] {
            return Object.keys(this.constructor.prototype).filter(name => {
                return typeof Object.getOwnPropertyDescriptor(this.constructor.prototype, name)["get"] === "function"
            });
        }
    
        public getSetters(): string[] {
            return Object.keys(this.constructor.prototype).filter(name => {
                return typeof Object.getOwnPropertyDescriptor(this.constructor.prototype, name)["set"] === "function"
            });
        }
    }
    

    The change is that instead of using this.prototype you're using this.constructor.prototype.
    Then you simply:

    let a = new Test();
    a.getGetters(); // ["RowsCount", "RowsCount2"]
    

    (code in playground)


    Edit

    Based on a comment by @Twois, who pointed out that it won't work when targetting es6, here's a version that will work:

    class Base {
        public static getGetters(): string[] {
            return Reflect.ownKeys(this.prototype).filter(name => {
                return typeof Reflect.getOwnPropertyDescriptor(this.prototype, name)["get"] === "function";
            }) as string[];
        }
    
        public static getSetters(): string[] {
            return Reflect.ownKeys(this.prototype).filter(name => {
                return typeof Reflect.getOwnPropertyDescriptor(this.prototype, name)["set"] === "function";
            }) as string[];
        }
    }
    

    The main difference: using Reflect.ownKeys(this.prototype) instead of Object.keys(this.prototype).

提交回复
热议问题