如何创建一个包含仅在 *.ts
文件中声明接口的定义文件的 NPM 包。
假设我们有两个接口和一个类定义:
export interface A {
id: number;
}
export interface B {
name: string;
}
export class C {
}
我需要将这些*.ts
文件打包到npm包中,应该如何操作?我应该在index.ts
中导出它们吗?
我的package.json
文件如下:
{
"name": "npm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
我的 tsconfig.json
文件如下:
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
在
index.ts
文件中有以下内容:import { A } from "./a";
import { B } from "./b";
import { C } from "./c";
其中'./a', './b', './c'
是包含接口和类声明的文件。
当我使用命令tsc index.ts
将其构建为index.js
文件时,我不能在其他项目(npm install)中使用模块index.js
来访问接口。