首先我有一个修饰函数的函数
function decorator(fn: Function): Function { /* ... */ }
然后能使用这个decorator的函数必须返回特定的类型(比如Promise)
interface IBaseFunc extends Function {
(...args): Promise;
}
修饰器返回的函数会有新的属性
interface IDecoratedFunc extends IBaseFunc { /* ... */ }
所以新的decorator声明为
function decorator(fn: IBaseFunc): IDecoratedFunc { /* ... */ }
但是使用这样的修饰器会丢失函数的参数表(全变成了...args
)
所以我使用了泛型
function decorator<F extends IBaseFunc>(fn: F): IDecoratedFunc<F> { /* ... */ }
现在问题来了,IDecoratedFunc
的声明该怎么写?
我是像下面这样写的,但会报TS2312: An interface may only extend a class or another interface.
interface IDecoratedFunc<F extends IBaseFunc> extends F { /* ... */ }