Typescript 从方法装饰器获取方法的签名
我有这样一个方法装饰器:Typescript 从方法装饰器获取方法的签名,typescript,typescript2.0,typescript-decorator,Typescript,Typescript2.0,Typescript Decorator,我有这样一个方法装饰器: export function NumberMethodDecorator(message: string) { return (target: object, propertyKey: string, descriptor?: TypedPropertyDescriptor<any>) => { // do some stuff here } } 但是,我想确保numbermethodecorator仅应用于签名为(value:nu
export function NumberMethodDecorator(message: string) {
return (target: object, propertyKey: string, descriptor?: TypedPropertyDescriptor<any>) => {
// do some stuff here
}
}
但是,我想确保numbermethodecorator
仅应用于签名为(value:number)=>any
的方法
如何执行此操作?在
TypedPropertyDescriptor
的类型参数中指定:
export function NumberMethodDecorator(message: string) {
return (
target: object, propertyKey: string,
descriptor?: TypedPropertyDescriptor<(value: number) => any>
) => {
// do some stuff here
};
}
export function NumberMethodDecorator(message: string) {
return (
target: object, propertyKey: string,
descriptor?: TypedPropertyDescriptor<(value: number) => any>
) => {
// do some stuff here
};
}
class SomeClass {
@NumberMethodDecorator("") // ok
someMethod(value: number) {
}
@NumberMethodDecorator("") // compile error
otherMethod() {
}
}