How to define static property in TypeScript interface

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

    I implemented a solution like Kamil Szot's, and it has an undesired effect. I have not enough reputation to post this as a comment, so I post it here in case someone is trying that solution and reads this.

    The solution is:

    interface MyInterface {
        Name: string;
    }
    
    const MyClass = class {
        static Name: string;
    };
    

    However, using a class expression doesn't allow me to use MyClass as a type. If I write something like:

    const myInstance: MyClass;
    

    myInstance turns out to be of type any, and my editor shows the following error:

    'MyClass' refers to a value, but is being used as a type here. Did you mean 'typeof MyClass'?ts(2749)
    

    I end up losing a more important typing than the one I wanted to achieve with the interface for the static part of the class.

    Val's solution using a decorator avoids this pitfall.

提交回复
热议问题