【问题标题】:Conversion of module.exports from javascript to typescript将 module.exports 从 javascript 转换为 typescript
【发布时间】:2019-06-20 08:26:20
【问题描述】:

我有 Node-express 代码,其中使用 module.exports 导出模块。比如导出一个函数,写成module.exports = functionName。现在代码将转换为打字稿。如何替换 typescript 中的 module.exports?

【问题讨论】:

    标签: javascript node.js typescript express


    【解决方案1】:

    只需使用export 后跟典型声明即可,无论是constfunctioninterfaceenum,都可以。

    export const myTestConst = () => console.log('hello world');
    

    https://www.typescriptlang.org/docs/handbook/modules.html

    【讨论】:

      【解决方案2】:

      加起来就是 duschsocke 的答案。您还可以使用公共方法创建一个类,并在需要这些方法的地方导入该类。

      utils.ts

      class Utils {
      
        public static publicFunction() {
          console.log('calling me');
        }
      }
      

      在其他 ts 文件上:

      import * as utils from 'utils';
      
      // Call the function
      utils.publicFunction(); // Prints 'calling me' on console.
      

      【讨论】:

        【解决方案3】:

        在 TypeScript 中,使用 export = functionName

        【讨论】:

          猜你喜欢
          • 2021-12-24
          • 2016-11-18
          • 2022-11-03
          • 2022-07-01
          • 2023-02-05
          • 1970-01-01
          • 1970-01-01
          • 2021-05-04
          • 2013-04-18
          相关资源
          最近更新 更多