如何在 ./node_modules/@types 之外使用打字稿定义文件

Posted

技术标签:

【中文标题】如何在 ./node_modules/@types 之外使用打字稿定义文件【英文标题】:How to use typescript definition files outside of ./node_modules/@types 【发布时间】:2018-03-17 17:58:00 【问题描述】:

我想改进安装在node_modules/@types 的软件包,但我不想在那里编辑它,因为node_modules 可能会被删除和/或更新。当我将文件夹移出node_modules/@types 时,VSCode 中的智能感知不再起作用。我已经编辑了tsconfig 中的文件、路径、包含和typeRoots 字段以匹配新位置,但没有运气。有谁知道如何设置 VSCode 和/或 tsconfig 以在将文件夹移出 node_modules/@types 时让智能感知工作?

【问题讨论】:

将文件移出文件夹时智能不起作用的原因是您已在该文件夹中安装了相应的定义文件。请将 d.ts 文件安装在 TS 的全局路径中。希望这对您有所帮助。如果不让我知道,我会提供进一步的帮助。 【参考方案1】:

模块扩充:https://www.typescriptlang.org/docs/handbook/declaration-merging.html

滚动到底部。

【讨论】:

以上是关于如何在 ./node_modules/@types 之外使用打字稿定义文件的主要内容,如果未能解决你的问题,请参考以下文章

不允许操作 mkdir \path\\node_modules\\@types\''

node_modules/@types/ 错误(接口“元素”不能同时扩展类型“ReactElement<any>)

SPFx Webpart - node_modules/@types/ [prop types] 和 [react] index.d.ts:在 gulp 构建时加载“错误 TS1005”

node_modules/@types/node/index.d.ts(20,1):错误 TS1084:无效的“参考”指令语法

node_modules/@types/react-dom/.... 的错误。后续的变量声明必须具有相同的类型。变量“a”

使用 forge-viewer/node_modules/@types/three 中的 THREE 会导致编译器错误