public static const in TypeScript

前端 未结 8 899
慢半拍i
慢半拍i 2020-12-07 10:11

Is there such a thing as public static constants in TypeScript? I have a class that looks like:

export class Library {
  public static BOOK_SHELF_NONE: stri         


        
8条回答
  •  生来不讨喜
    2020-12-07 10:42

    You can use a getter, so that your property is going to be reading only. Example:

    export class MyClass {
        private _LEVELS = {
            level1: "level1",
            level2: "level2",
            level2: "level2"
        };
    
        public get STATUSES() {
            return this._LEVELS;
        }
    }
    

    Used in another class:

    import { MyClass } from "myclasspath";
    class AnotherClass {
        private myClass = new MyClass();
    
        tryLevel() {
           console.log(this.myClass.STATUSES.level1);
        }
    }
    

提交回复
热议问题