javascript - 使用 TypeScript 冻结导出
问题描述
我认为这是最好的方法:
export const stdMarker = '@json-stdio';
export const stdEventName = '@json-stdio-event';
Object.defineProperties(module.exports, {
'stdMarker': {
writable: false
},
'stdEventName': {
writable: false
}
});
有什么速记吗?如果 TS 提供类似的东西会很有趣:
export frozen const stdMarker = '@json-stdio';
export frozen const stdEventName = '@json-stdio-event';
管他呢。有一个更好的方法吗?
解决方案
Since you've defined stdMarker
and stdEventName
as const
(and their value is a primitive, not an object) you should have the desired behavior out of the box.
When importing the const in another file, and attempting to modify it, I get an error:
Cannot assign to 'test' because it is not a variable.
What behavior do you expect from a frozen export? Your defineProperties call is doing a similar thing to const
in this situation.
推荐阅读
- java - Firestore Query 从控制台工作,但不能从代码工作
- javascript - jQuery 获取最近的 li 的 id
- flutter - 如何在 Flutter 中用星号替换部分字符串?
- mongodb - 我不知道如何在 Mongo 的同一语句中使用 $ eq 和 $ regex
- c++ - 根据另一个向量对一个向量进行排序
- java - 未找到 Tomcat 服务,但网站有效
- node.js - 如何在aws sqs node js中接收消息
- c++ - C++ 中的标点符号是什么?
- java - 将 Firestore 中的字符串保存在 Java 的 ArrayList 中
- java - 这会被定义为 HeapSort 的一种形式吗?