How to implement class constants?
@lebyy▲ 0 net votes
Problem: 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 'const' keyword." I find myself in need to clearly indicate in code that a property should not be… Fix (from the top answer): TypeScript 2.0 has the readonly modifier : class MyClass { readonly myReadOnlyProperty = 1; myMethod() { console.log(this.myReadOnlyProperty); this.myReadOnlyPrope It's not exactly a constant because it allows assignment in the constructor, but that's most likely not a big deal. Alternative Solution An alternative is…