public static const in TypeScript

前端 未结 8 850
慢半拍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:43

    Thank you WiredPrairie!

    Just to expand on your answer a bit, here is a complete example of defining a constants class.

    // CYConstants.ts
    
    class CYConstants {
        public static get NOT_FOUND(): number    { return -1; }
        public static get EMPTY_STRING(): string { return ""; }
    }
    
    export = CYConstants;
    

    To use

    // main.ts
    
    import CYConstants = require("./CYConstants");
    
    console.log(CYConstants.NOT_FOUND);    // Prints -1
    console.log(CYConstants.EMPTY_STRING); // Prints "" (Nothing!)
    

提交回复
热议问题