How to implement class constants?

后端 未结 8 1848
爱一瞬间的悲伤
爱一瞬间的悲伤 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:42

    Constants can be declare outside of classes and use within your class. Otherwise the get property is a nice workaround

    const MY_CONSTANT: string = "wazzup";
    
    export class MyClass {
    
        public myFunction() {
    
            alert(MY_CONSTANT);
        }
    }
    

提交回复
热议问题