在接口typescript中支持静态和非静态方法
所以很明显(我是初学者)ts不支持界面中的静态方法,但是有一个解决方法解释了它的答案。当您的类只有静态方法时,它可以工作。但是,如果我的类是静态方法和非静态方法的组合,这将抛出一个错误:在接口typescript中支持静态和非静态方法,typescript,Typescript,所以很明显(我是初学者)ts不支持界面中的静态方法,但是有一个解决方法解释了它的答案。当您的类只有静态方法时,它可以工作。但是,如果我的类是静态方法和非静态方法的组合,这将抛出一个错误: Class 'MyClass' incorrectly implements interface 'sampleInterface'. Property 'staticFunction' is missing in type 'MyClass' but required in type 'sampleInt
Class 'MyClass' incorrectly implements interface 'sampleInterface'.
Property 'staticFunction' is missing in type 'MyClass' but required in type 'sampleInterface'
你知道如何支持这一点吗
export function staticDecorator<T>() {
return (constructor: T) => {};
}
interface sampleInterface {
staticFynction(/*something*/): promise<void>;
nonStaticFynction(/*something*/): promise<void>;
}
@staticDecorator()
class MyClass implements sampleInterface {
public static staticFynction(/*something*/): promise<void>{
//something
}
public nonStaticFynction(/*something*/): promise<void>{
//something
}
}
导出函数staticDecorator(){
返回(构造函数:T)=>{};
}
接口采样接口{
静态动作(/*某物*/):承诺;
非静态的(/*某物*/):承诺;
}
@staticDecorator()
类MyClass实现sampleInterface{
公共静态静态功能(/*某物*/):承诺{
//某物
}
公共非固定化(/*某物*/):承诺{
//某物
}
}
通常需要两个接口;一个用于类的实例端,一个用于类的静态端。例如:
interface SampleInterfaceInstancePart {
nonStaticFunction(/*something*/): Promise<void>;
}
interface SampleInterfaceStaticPart {
staticFunction(/*something*/): Promise<void>;
}
希望有帮助;祝你好运
@staticDecorator<SampleInterfaceStaticPart>()
class MyClass implements SampleInterfaceInstancePart {
public static async staticFunction(/*something*/): Promise<void> { }
public async nonStaticFunction(/*something*/): Promise<void> { }
}