【问题标题】:Typescript npm library打字稿 npm 库
【发布时间】:2018-11-10 19:56:09
【问题描述】:

我在 typescript 中有一堆基类。我还有一个使用 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 编译器的 sn-p 提供了一个捆绑文件: “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 应用程序。我看不到我错过了什么。

提前致谢!

【问题讨论】:

  • 你编译好的js文件写到哪里了?

标签: typescript npm


【解决方案1】:

【讨论】:

  • 谢谢!这基本上是正确的问题。由于 package.json 需要一个条目,我只是使用了一些基类,它确实像一个魅力!不再将所有文件捆绑到一个巨型文件中,它现在更加结构化。
猜你喜欢
  • 2017-11-13
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2023-01-28
  • 1970-01-01
  • 2016-06-17
  • 2021-09-03
相关资源
最近更新 更多