TypeScript 中的公共静态常量 [英] public static const in TypeScript
问题描述
TypeScript 中是否有公共静态常量之类的东西?我有一个看起来像这样的课程:
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: string = "None";
public static BOOK_SHELF_FULL: string = "Full";
}
在那堂课上,我可以做 Library.BOOK_SHELF_NONE
并且 tsc 没有抱怨.但是如果我尝试在其他地方使用类库,并尝试做同样的事情,它就无法识别它.
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.
推荐答案
这是编译成的 TS 代码段的内容(通过 TS Playground):
Here's what's this TS snippet compiled into (via TS Playground):
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;
});
如您所见,定义为 public static
的两个属性都简单地附加到导出的函数(作为其属性);因此,只要您正确访问函数本身,它们就应该是可访问的.
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.
这篇关于TypeScript 中的公共静态常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!