How to implement class constants?

后端 未结 8 1852
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-22 17:40

In TypeScript, the const keyword cannot be used to declare class properties. Doing so causes the compiler to an error with \"A class member cannot have the \'co

8条回答
  •  既然无缘
    2020-12-22 18:18

    You can mark properties with readonly modifier in your declaration:

    export class MyClass {
      public static readonly MY_PUBLIC_CONSTANT = 10;
      private static readonly myPrivateConstant = 5;
    }
    

    @see TypeScript Deep Dive book - Readonly

提交回复
热议问题