typescript - 打字稿 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 应用程序。我看不到我错过了什么。
提前致谢!
解决方案
tsconfig.json 如下所示:
首选官方文档中提到的约定:https ://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html
推荐阅读
- mysql - 连接 3 个表的 MySQL 查询给出错误的结果
- sql - 如何识别小数点后只有零的记录?
- r - 在 r 中的列表列中的所有列表中将 numeric(0) 替换为 NA
- android - 如何调整正在使用的图表百分比?
- css - 将横向和纵向图像放置在相同的高度
- python-3.x - 在 Python 子进程中打开 Shell
- php - 停止“成员具有受保护的访问权限”消息
- android - 如何在发布 apk 中删除/禁用 objectbox 数据浏览器服务?
- php - 在 laravel 中使用 slug 创建帖子时出现重复条目
- reactjs - 无法读取 null firebase 的属性票