javascript - 从 node_modules index.js 导入类定义
问题描述
我的项目在其 node_modules 中有包“diagram-js”。该文件node_modules/diagram-js/lib/model/index.js
有几个类定义,如下所示:
/**
* @namespace djs.model
*/
/**
* @memberOf djs.model
*/
/**
* The basic graphical representation
*
* @class
*
* @abstract
*/
export function Base() { ... }
我尝试通过以下方式使用智能感知使这些类在我的项目中可导入:
- 创建了文件
@types\diagram-js\lib\model\index.d.ts
- 尝试了以下任何语法:
export module 'diagram-js/lib/model' { ... }
export namespace djs.model { ... }
export declare namespace djs.model { ... }
在所有情况下,最外面的块都包含这样的类定义:
declare class Base {
id: string;
type: string;
businessObject: any;
label: Object;
parent: Shape;
labels: Array<Label>;
outgoing: Array<Connection>;
incoming: Array<Connection>;
}
我也尝试过declare module
在其中export class
,但是虽然这适用于其他没有的文件,但index.js
在这种情况下,智能感知似乎无法找到这些类。
目前我正在使用该namespace
解决方案,但导入看起来../../../../@types/diagram-js/lib/model
不是我想要的。
在我的代码库中公开这些类并获得智能感知的正确方法是什么?
解决方案
看起来我需要做的就是将文件所在的文件夹index.d.ts
添加typeRoots
到tsconfig
. 对于名称index.js
与进程不同的文件,这让我感到困惑。
推荐阅读
- excel - 另存为提示位置但使用单元格中的文件名
- android - android recyclerview swipe 生成 java.lang.IndexOutOfBoundsException: Invalid index
- android - 带抽屉布局的安卓喷气背包
- corda - 在 Corda 中,响应者如何将组件添加到事务中?
- java - 当 find(Object.Class, {CompositeKey}) 方法的键是复合键时如何查找引用?
- php - 从用户中删除元
- arrays - 在处理中从数组中提取图像
- swift - 如何在 Firebase 数据库 Swift 中进行搜索
- android - 带有 StrongBox 的 Android KeyStore:如何测试用户存在?
- jquery - 从 Ajax 调用控制器操作时无法下载生成的 PDF