How to define static property in TypeScript interface

前端 未结 14 1274
你的背包
你的背包 2020-11-28 05:49

I just want to declare a static property in typescript interface? I have not found anywhere regarding this.

interface myInterface {
  static         


        
14条回答
  •  悲哀的现实
    2020-11-28 06:21

    The other solutions seem to stray from the blessed path and I found that my scenario was covered in the Typescript documentation which I've paraphrased below:

    interface AppPackageCheck {
      new (packageExists: boolean): T
      checkIfPackageExists(): boolean;
    }
    
    class WebApp {
        public static checkIfPackageExists(): boolean {
            return false;
        }
    
        constructor(public packageExists: boolean) {}
    }
    
    class BackendApp {
        constructor(public packageExists: boolean) {}
    }
    
    function createApp(type: AppPackageCheck): T {
        const packageExists = type.checkIfPackageExists();
        return new type(packageExists)
    }
    
    let web = createApp(WebApp);
    
    // compiler failure here, missing checkIfPackageExists
    let backend = createApp(BackendApp); 
    

提交回复
热议问题