Using a Decorator to get list of implemented interfaces

前端 未结 2 1859
我寻月下人不归
我寻月下人不归 2021-02-05 23:30

Do you know if it is possible to get the array of interfaces implemented by a class using a decorator:

interface IWarrior {
  // ...
}

interface INinja {
  // .         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 00:02

    Currently, types are used only during development and compile time. The type information is not translated in any way to the compiled JavaScript code. But you however can pass the list of strings to the decorator parameter like this:

    interface IWarrior {
      // ...
    }
    
    interface INinja {
      // ...
    }
    
    
    interface Function {
        interfacesList: string[];
    }
    
    @interfaces(["INinja", "IWarrior"])
    class Ninja implements INinja, IWarrior {
    
    }
    
    function interfaces(list: string[]) {
        return (target: any) => {
            target.interfacesList = list; 
            return target;
        }
    }
    
    console.log(Ninja.interfacesList);
    

提交回复
热议问题