小编wiu*_*lma的帖子

从模块导出类型

我用 ts 编写代码已经有一段时间了,但我仍然不明白女巫是从模块导出函数和类型的正确方法。

这是上下文:

  • Typescript 2.7,节点 9。

    1. 具有导出函数和类型的语言环境模块 A
    2. 模块 B 添加模块 A 作为依赖项,并且它使用从 A 导出的函数和类型

在我的项目中,当我声明类型时,我更喜欢使用 @types 文件夹和 d.ts 文件。例如:

declare type Message = {
   message: string
}
Run Code Online (Sandbox Code Playgroud)

这样使用时就不需要显式导入类型了。

但我不明白如何导出它。我导出类型的唯一方法是使用接口。

export interface IMessage {
    message: string
}
Run Code Online (Sandbox Code Playgroud)

但这样我就必须在需要时在 ts 文件中导入 then 接口,我想避免这种情况。

你能帮助我理解正确的方法吗?

module node.js typescript

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

module ×1

node.js ×1

typescript ×1