【发布时间】:2018-07-04 01:18:28
【问题描述】:
在 JS 中,我的很多模块只是包装静态函数、枚举和属性的对象。例如,我有一个模块Debug 与此类似(我确实简化了它):
export default Debug = {
// Current mode set, enum value from Modes. Setted outside of the module (see below).
mode : null,
// Enum of all possible modes
Modes : {
DEV : 0,
PROD : 1,
},
// getter, checks if it's in production mode
get isInProdMode() {
return Debug.mode === Debug.Modes.PROD;
},
// If the condition is met, it will throw an error in development mode, or just silently log a warning in production mode
assert : function(condition, message){
if (condiftion) {
if (Debug.isInProdMode)
console.warn(message);
else
throw message;
}
}
}
// index.js
Debug.mode = Debug.Modes.DEV;
如何在 Typescript 中创建这样的匿名对象?使用枚举作为属性?还有一个getter函数?所有属性都是已知的。 我真的被困住了。
【问题讨论】:
-
此代码已经是有效的打字稿。你还需要它做什么?
标签: javascript typescript