typescript - 如何防止重复的自动建议导入

标签 typescript visual-studio-code typescript-typings

假设我有一些像这样的源代码

// index.js
import xxx from "./lib/xxx"
export { xxx }
// lib/xxx.js
export default function() {}

tsc编译后会生成两个文件

// index.d.ts
import xxx from "./lib/xxx"
export { xxx }
// lib/xxx.d.ts
export default function(): void;

然后假设我发布这个包并命名为ex,当我安装ex时,在代码中使用将获得导入的自动建议,但我只需要一个自动-导入建议,有人可以告诉我该怎么做吗?

enter image description here

enter image description here

最佳答案

您可能想要 Typescript 早期的“内部模块”,它使用 module 关键字来包装内容。参见这里:https://fizzylogic.nl/2016/02/07/typescript-internal-vs-external-modules/

摘自文章:

Internal modules are really namespaces, which is model that is most often applied in the browser. There's a big need for namespaces in a web application since everything naturally gets loaded into the window scope. The window scope can turn into a massive pile of variables if you don't manage your scripts carefully. This is where internal modules or namespaces really shine.

Typescript 的 module 关键字早于实际的 ECMAscript 模块。

关于typescript - 如何防止重复的自动建议导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63917115/

相关文章:

jquery - 如何获取特定版本的 Jquery(例如 3.2.1)的 TypeScript 定义文件?

typescript 2 : custom typings for untyped npm module

html - 使用内联样式传递innerHtml(Angular 10)

c# - 使用 Visual Studio Code 在 Linux 上编写 C#

在 Visual Studio Code 中调试时 Firefox 未打开

python - 如何使用 Anaconda 安装 Visual Studio Code IDE?

typescript - 如何解压作为 Typescript 联合且可能是数组的返回类型?

angular - 子组件不更新父路由更改

javascript - javascript 中缺少 typescript 声明文件

angular - 错误 : "Encountered undefined provider! Usually this means you have a circular dependencies"