How to define static property in TypeScript interface

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

    Static properties are usually placed on the (global) constructor for the object, whereas the "interface" keyword applies to instances of the object.

    The previous answer given is of course correct if you are writing the class in TypeScript. It may help others to know that if you are describing an object that is already implemented elsewhere, then the global constructor including static properties can be declared like this:

    declare var myInterface : {
      new(): Interface;
      Name:string;
    }
    
    0 讨论(0)
  • 2020-11-28 06:00

    @duncan's solution above specifying new() for the static type works also with interfaces:

    interface MyType {
        instanceMethod();
    }
    
    interface MyTypeStatic {
        new():MyType;
        staticMethod();
    }
    
    0 讨论(0)
  • 2020-11-28 06:03

    If you're looking to define a static class (ie. all methods/properties are static), you can do something like this:

    interface MyStaticClassInterface {
      foo():string;
    }
    
    var myStaticClass:MyStaticClassInterface = {
      foo() {
        return 'bar';
      }
    };
    

    In this case, the static "class" is really just a plain-ol'-js-object, which implements all the methods of MyStaticClassInterface

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-28 06:06

    Though static keyword not supported in interface in Typescript but we can achieve it by creating a function interface that has static member.

    In my following code I have created a function interface Factory that has two static members serialNumber and printSerial.

    // factory is a function interface
    interface Factory<T> {
        (name: string, age: number): T;
    
        //staic property
        serialNumber: number;
    
        //static method
        printSrial: () => void;
    }
    
    class Dog {
        constructor(public name: string, public age: number) { }
    }
    
    const dogFactory: Factory<Dog> = (name, age) => {
        return new Dog(name, age);
    }
    
    // initialising static members
    
    dogFactory.serialNumber = 1234;
    dogFactory.printSrial = () => console.log(dogFactory.serialNumber);
    
    
    //instance of Dog that DogFactory creates
    const myDog = dogFactory("spike", 3);
    
    //static property that returns 1234
    console.log(dogFactory.serialNumber)
    
    //static method that prints the serial 1234
    dogFactory.printSrial();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题