【问题标题】:How can I import a CommonJS module that exports a single function from TypeScript如何导入从 TypeScript 导出单个函数的 CommonJS 模块
【发布时间】:2019-01-30 17:20:28
【问题描述】:

鉴于这个 CommonJS 模块:

// cjs.js
module.exports = () => console.log("hi");

我可以在我的 .d.ts 中添加什么

// cjs.d.ts
export ????

这样我就可以从 TypeScript 文件中进行这样的星号导入并选择正确的类型:

// main.ts
import * as log from "./cjs"

log()

【问题讨论】:

    标签: typescript commonjs es6-modules


    【解决方案1】:

    你可以使用导出赋值,但你需要声明一个中介const,它将被输入为函数:

    declare const def: () => void
    export = def;
    

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 2016-07-30
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多