首页 > 解决方案 > 从 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() { ... }

我尝试通过以下方式使用智能感知使这些类在我的项目中可导入:

  1. 创建了文件@types\diagram-js\lib\model\index.d.ts
  2. 尝试了以下任何语法:
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不是我想要的。

在我的代码库中公开这些类并获得智能感知的正确方法是什么?

标签: javascripttypescriptnpmnode-modulestypescript-typings

解决方案


看起来我需要做的就是将文件所在的文件夹index.d.ts添加typeRootstsconfig. 对于名称index.js与进程不同的文件,这让我感到困惑。


推荐阅读