首页 > 解决方案 > 打字稿 npm 库

问题描述

我在打字稿中有一堆基类。我还有一个使用 typescript 编写的 web 应用程序,我想在其中包含定义所有数据类型的“普通旧 Typescript 对象”库。让我们称它为“domain.common”库。

从 domain.commin 我有一个小的 npm 构建任务,它基本上使用 typescript 编译器和配置来创建实际的 js 代码。tsconfig.json 如下所示:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "amd",
    "sourceMap": false,
    "declaration": true,
    "outFile": "npm_lib_template/bundle.js"
  },
  "include": [
    "./src/**/*.ts"
  ]
}

这个使用 typescript 编译器的代码片段提供了一个捆绑文件:“bundle.js”和“bundle.d.ts”。这一切都很好,并且偷看声明文件似乎遵循导出打字稿文件的标准。

declare module "BaseEntity" {
    /**
     * The BaseEntity class that contains an unique ID
     * @class
     */
    export abstract class BaseEntity {
        /**
         * The unique identifier for this entity
         */
        id: string;
        classType: string;
        equals(obj: any): boolean;
        constructor();
    }
}
declare module "BaseConnection" {
    import { BaseEntity } from "BaseEntity";
    /**
     * A base connection class that represent a link between two {@link BaseEntity} classes
     * @class
     * @extends BaseEntity
     */
    export abstract class BaseConnection extends BaseEntity {
        nodeFirstReference: string;
        nodeSecondReference: string;
        constructor();
    }
}
declare module "housing/HouseInformation" {
    import { BaseEntity } from "BaseEntity";
    export class HouseInformation extends BaseEntity {
        address: string;
        zipCode: string;
        city: string;
        country: string;
        constructor();
    }
}

我可以在 web 应用程序中确认这一点,看到 Visual Studio Code 选择了类定义。

import {HouseInformation} from 'housing/HouseInformation'

但是当我启动应用程序时,我得到:

找不到模块:无法解析“...\src\service\data”中的“HouseInformation”

此外,我在开发过程中使用 npm link 将本地库链接到 Web 应用程序。我看不到我错过了什么。

提前致谢!

标签: typescriptnpm

解决方案



推荐阅读