patterntypescriptModerateCanonical
public static const in TypeScript
Viewed 0 times
typescriptpublicconststatic
Problem
Is there such a thing as public static constants in TypeScript? I have a class that looks like:
In that class, I can do
export class Library {
public static BOOK_SHELF_NONE: string = "None";
public static BOOK_SHELF_FULL: string = "Full";
}In that class, I can do
Library.BOOK_SHELF_NONE and the tsc doesn't complain. But if I try to use the class Library elsewhere, and try to do the same thing, it doesn't recognize it.Solution
Here's what's this TS snippet compiled into (via TS Playground):
As you see, both properties defined as
define(["require", "exports"], function(require, exports) {
var Library = (function () {
function Library() {
}
Library.BOOK_SHELF_NONE = "None";
Library.BOOK_SHELF_FULL = "Full";
return Library;
})();
exports.Library = Library;
});As you see, both properties defined as
public static are simply attached to the exported function (as its properties); therefore they should be accessible as long as you properly access the function itself.Code Snippets
define(["require", "exports"], function(require, exports) {
var Library = (function () {
function Library() {
}
Library.BOOK_SHELF_NONE = "None";
Library.BOOK_SHELF_FULL = "Full";
return Library;
})();
exports.Library = Library;
});Context
Stack Overflow Q#22991968, score: 27
Revisions (0)
No revisions yet.