如何解决打字稿中装饰器的别名
我知道类型有别名:
type ShortTypeName = LooooooooooongTypeName
现在我试图用装饰器来简化我复杂的打字稿代码,我对它不是很熟悉。假设我有以下代码:
class Foo {
@SomeDecorator(someConst1,someConst2,someConst3,someConst4)
property1: string
@SomeDecorator(someConst1,someConst4)
property2: number
@SomeDecorator(someConst1,someConst4)
property3: Date
@SomeDecorator(someConst1,someConst4)
property4: Bar
}
我想使用“别名”以便我可以这样写:
alias D = SomeDecorator(someConst1,someConst4)
class Foo {
@D
property1: string
@D
property2: number
@D
property3: Date
@D
property4: Bar
}
我知道这样的别名不存在,但我想这种简化可以通过声明新的装饰器(即声明新函数)D
来完成,它调用 SomeDecorator
。我该怎么做?
解决方法
好的,我找到了解决方案。我可以像下面的代码一样定义 D
。
function D(target: any,propertyName: string) {
const f = SomeDecoration(someConst1,someConst2,someConst3,someConst4)
f(target,propertyName)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。