【问题标题】:Freezing exports with TypeScript使用 TypeScript 冻结导出
【发布时间】:2018-12-16 05:18:20
【问题描述】:

我认为这是最好的方法:

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';

或其他。有没有更好的方法来做到这一点?

【问题讨论】:

标签: javascript node.js typescript typescript2.0 tsc


【解决方案1】:

由于您已将 stdMarkerstdEventName 定义为 const(并且它们的值是原始值,而不是对象),因此您应该具有开箱即用的所需行为。 在另一个文件中导入 const 并尝试对其进行修改时,出现错误:

Cannot assign to 'test' because it is not a variable.

您期望冻结导出的行为是什么?在这种情况下,您的 defineProperties 调用与 const 执行类似的操作。

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    相关资源
    最近更新 更多