【发布时间】:2015-12-12 19:58:05
【问题描述】:
我正在尝试如下实现一个 Typescript 方法装饰器。
function dataMethod(name: string, options: any) {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
}
}
它的用法如下。
class HelloWidgetExtesion {
@dataMethod("getData", {})
public getData(name: any, cb: any) {
cb(null, "");
}
}
但我正在尝试弄清楚如何使用装饰器和箭头函数实现,如下所示。
class HelloWidgetExtesion {
@dataMethod("getData", {})
public getData = (name: any, cb: any) => {
cb(null, "Greetings from Loopback!");
}
}
但是上面的实现在编译时会出现如下错误。
错误TS2322:类型'(目标:任何,propertyKey:字符串,描述符: TypedPropertyDescriptor) => void' 不可分配给类型 '(target: Object, propertyKey: string | symbol) => void'.
【问题讨论】: