Как я могу объединить декоратор класса и декоратор свойства в Typescript? - PullRequest
0 голосов
/ 19 февраля 2020

В настоящее время у меня есть:


function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) {
    return class extends constructor {
        newProperty = "new property";
        hello = "override";
    }
}

function logType(target : any, key : string) {
    var t = Reflect.getMetadata("design:type", target, key);
    console.log(`${key} type: ${t.name}`);
  }

@classDecorator
class Data {
    @logType
    station: number;
    @logType
    name: string;
    @logType
    letters: string;
    constructor(station: number, name: string, letters: string) {
        this.station = station;
        this.name = name;
        this.letters = letters;
    }
}

, в котором декоратор logType получает тип каждого оформленного свойства. Что меня интересует, так это то, что мне удастся как-то объединить его с classDecorator таким образом, чтобы я мог:

  • Получить из всего класса все имя и тип свойств всякий раз, когда classDecorator называется.
...