从node_modules index.js

时间:2020-07-28 06:12:54

标签: javascript typescript npm node-modules typescript-typings

我的项目的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() { ... }

我尝试通过以下方式使这些类可在intellisense中导入项目:

  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的文件一起使用,但是在这种情况下,Intellisense似乎找不到课程。

当前我正在使用namespace解决方案,但是导入看起来像../../../../@types/diagram-js/lib/model,这不是我想要的。

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

1 个答案:

答案 0 :(得分:0)

看来我要做的就是将index.d.ts文件所在的文件夹添加到typeRoots中的tsconfig属性中。对于名称不是index.js的文件,处理过程有所不同,这让我感到困惑。