打字稿中装饰器的别名

Alias for decorator in typescript

我知道类型有别名:

type ShortTypeName = LooooooooooongTypeName

现在我正在尝试使用我不太熟悉的装饰器来简化我复杂的打字稿代码。假设我有以下代码:

class Foo {
    @SomeDecorator(someConst1, someConst2, someConst3, someConst4)
    property1: string
    @SomeDecorator(someConst1, someConst2, someConst3, someConst4)
    property2: number
    @SomeDecorator(someConst1, someConst2, someConst3, someConst4)
    property3: Date
    @SomeDecorator(someConst1, someConst2, someConst3, someConst4)
    property4: Bar
}

我想使用“别名”,这样我就可以这样写:

alias D = SomeDecorator(someConst1, someConst2, someConst3, 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)
}