我想为typescript中的类装饰器定义一个装饰器签名,用于已存在的JavaScript代码。比方说,我有一些已经定义的类Foo
具有所需方法的类装饰器签名
class Foo {
someMethod() {
}
}
然后也有一些它需要输入类类的装饰方法,但它要求输入类有它的装饰之前定义的其它一些方法。
function decorate(someClass) {
// some properties and methods injection happens here
}
然后在我的打字稿的代码,我将创建一个类来装饰为:
class Bar extends Foo {
requiredMethod(): any {
return null;
}
}
那么对于定义我想:
interface WithRequiredMethod {
requiredMethod(): any;
}
declare function decorate<C extends Foo>(input: C & WithRequiredMethod): C;
然后调用它为:
const DocoratedBar = decorate(Bar);
不幸的是typescript doesn'即使定义了requiredMethod
,也不会将Bar识别为有效输入。有什么方法可以定义decorate
签名来验证输入类中是否存在requiredMethod
?
谢谢,这是我忽视。 –