Typescript: constants in an interface

后端 未结 5 1317
一生所求
一生所求 2020-12-17 14:31

How do I place a constant in an Interface in typescript. Like in java it is:

interface OlympicMedal {
  static final String GOLD = \"Gold\";
  static final S         


        
5条回答
  •  遥遥无期
    2020-12-17 15:03

    There is a workaround for having constants in a interface: define both the module and the interface with the same name.

    In the following, the interface declaration will merge with the module, so that OlympicMedal becomes a value, namespace, and type. This might be what you want.

    module OlympicMedal {
        export const GOLD = "Gold";
        export const SILVER = "Silver";
    }
    
    interface OlympicMedal /* extends What_you_need */ {
        myMethod(input: any): any;
    }
    

    This works with Typescript 2.x

提交回复
热议问题