繁体   English   中英

从 node_modules index.js 导入 class 定义

[英]import class definitions from node_modules index.js

我的项目在其 node_modules 中有 package “diagram-js”。 文件node_modules/diagram-js/lib/model/index.js有几个 class 定义,如下所示:

/**
 * @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 { ... }

在所有情况下,最外面的块都包含 class 定义,如下所示:

  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文件所在的文件夹添加到tsconfigtypeRoots属性中。 对于名称不是index.js的文件,过程是不同的,这让我感到困惑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM