0
我有一个装饰器方法,我希望它只能用于异步方法。这是使用的例子:限制方法装饰器的使用
class A {
@deco() // This should work.
public async works() { }
@deco() // This should fail.
public fails() { }
}
我试图这样定义装饰:
export function deco() {
return <T extends {[K in keyof T]:() => Promise<any>},
K extends string>
(target: T,
propertyKey: K,
descriptor: PropertyDescriptor) => {
// Decorator code here.
};
}
但它不工作。它未能在这两个works
和fails
方法,因为K
[K in keyof T]
和K extends string
和propertyKey: K
的K
是不一样的,因此K
并不限定为T.
的关键这不起作用或者:
export function deco() {
return <T extends {[K in keyof T]:() => Promise<any>},
K extends keyof T>
(target: T,
propertyKey: K,
descriptor: PropertyDescriptor) => {
// Decorator code here.
};
}
无论是这样的:
export function deco() {
return <T,
K extends keyof T>
(target: T & {[propertyKey]:() => Promise<any>},
propertyKey: K,
descriptor: PropertyDescriptor) => {
// Decorator code here.
};
}
任何想法?
究竟是什么问题?使用PropertyDescriptor而不是TypedPropertyDescriptor? – lilezek
“PropertyDecorator”没有提供基于目标类型进行限制的方式,“MethodDecorator”是通用方法,泛型类型是目标类型。一般来说,因为你正在装饰一个方法,所以你应该使用一个'MethodDecorator'顾名思义 –