Extending type when extending an ES6 class with a TypeScript decorator

前端 未结 3 1639
栀梦
栀梦 2021-01-12 11:34

I am trying to decorate a class with a decorator (a-la-angular style), and add methods and properties to it.

this is my example decorated class:

         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 11:54

    To supplement jcalz response, going back to the definition of the Decorator Pattern, it does not change the interface/contracts of its target. It's not just terminology. TypeScript decorators share similarities with Java annotations and .NET attributes which are aligned with the fact not to change the interface: they just add metadata.

    Class mixin is a good candidate to solve your question. But it's better not to use "decorator" in its name in order to avoid confusion.

提交回复
热议问题