How to define static property in TypeScript interface

前端 未结 14 1267
你的背包
你的背包 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:05

    Another option not mentioned here is defining variable with a type representing static interface and assigning to it class expression:

    interface MyType {
        instanceMethod(): void;
    }
    
    interface MyTypeStatic {
        new(): MyType;
        staticMethod(): void;
    }
    
    // ok
    const MyTypeClass: MyTypeStatic = class MyTypeClass {
        public static staticMethod() { }
        instanceMethod() { }
    }
    
    // error: 'instanceMethod' is missing
    const MyTypeClass1: MyTypeStatic = class MyTypeClass {
        public static staticMethod() { }
    }
    
    // error: 'staticMethod' is missing
    const MyTypeClass2: MyTypeStatic = class MyTypeClass {
        instanceMethod() { }
    }
    

    The effect is same as in answer with decorators, but without overhead of decorators

    Playground

    Relevant suggestion/discussion on GitHub

提交回复
热议问题